Skip to content

Instantly share code, notes, and snippets.

@phpdave
Created January 10, 2017 03:48
Show Gist options
  • Save phpdave/44cd2f88c6c706029d803605f3b81bd2 to your computer and use it in GitHub Desktop.
Save phpdave/44cd2f88c6c706029d803605f3b81bd2 to your computer and use it in GitHub Desktop.
Hello World Go Lang
package main
import (
"fmt"
"github.com/phpdave/string"
)
func main() {
fmt.Printf(string.Reverse("\nHello I'm writing some go code\n"))
}
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)
}
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