Last active
December 20, 2015 10:39
-
-
Save tulios/6117781 to your computer and use it in GitHub Desktop.
Reverse string function in GO (study purpose)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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