Created
January 10, 2017 03:48
-
-
Save phpdave/44cd2f88c6c706029d803605f3b81bd2 to your computer and use it in GitHub Desktop.
Hello World Go Lang
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 main | |
import ( | |
"fmt" | |
"github.com/phpdave/string" | |
) | |
func main() { | |
fmt.Printf(string.Reverse("\nHello I'm writing some go code\n")) | |
} |
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 string | |
func Reverse(s string) string{ | |
//b:= []bytes(s) | |
//rune(s) used instead of byte(s) so UTF 8 characters work properly | |
b:= []rune(s) | |
for i := 0 ; i < len(b)/2; i++{ | |
j:= len(b)-i-1 | |
b[i], b[j] = b[j], b[i] | |
} | |
return string(b) | |
} |
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 string | |
import "testing" | |
func Test(t *testing.T) { | |
var tests = []struct { | |
s, want string | |
}{ | |
{"Backward","drawkcaB"}, | |
{"Hello You!", "!uoY olleH"}, | |
{"Hi क़","क़ iH"}, | |
} | |
for _, c := range tests { | |
got := Reverse(c.s) | |
if got != c.want { | |
t.Errorf("Reverse(%q) == %q, want %q", c.s, got, c.want) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment