Skip to content

Instantly share code, notes, and snippets.

@tulios
Last active December 20, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tulios/6117781 to your computer and use it in GitHub Desktop.
Save tulios/6117781 to your computer and use it in GitHub Desktop.
Reverse string function in GO (study purpose)
package str
func Reverse(input string) string {
rune_array := []rune(input)
size := len(rune_array) - 1
result := make([]rune, size + 1)
for i := 0; i <= size; i++ {
result[size - i] = rune_array[i]
}
return string(result)
}
package str
import (
"testing"
)
func AssertEqual(t *testing.T, output string, expected string) {
if output != expected {
t.Errorf("Expected %s, got %s", expected, output)
}
}
func TestReverse(t *testing.T) {
AssertEqual(t, Reverse("AB"), "BA")
AssertEqual(t, Reverse("casa"), "asac")
AssertEqual(t, Reverse("roma"), "amor")
AssertEqual(t, Reverse("Tulio"), "oiluT")
AssertEqual(t, Reverse("世界"), "界世")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment