Skip to content

Instantly share code, notes, and snippets.

@phpdave
Last active January 10, 2017 03:53
Show Gist options
  • Save phpdave/632469ad1355a2e3a99366c2d4446109 to your computer and use it in GitHub Desktop.
Save phpdave/632469ad1355a2e3a99366c2d4446109 to your computer and use it in GitHub Desktop.
#WorkSpace
export GOPATH=$HOME/work
#Binaries produced by GO
export PATH=$PATH:$GOPATH/bin
├── bin
│   └── hello
├── pkg
│   └── darwin_amd64
│   └── github.com
│   └── phpdave
│   └── string.a
└── src
└── github.com
└── phpdave
├── hello
│   └── hello.go
└── string
├── string.go
└── string_test.go
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