Skip to content

Commit

Permalink
feat: reverse echo
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyoucao577 committed Oct 7, 2019
1 parent 9871515 commit d401d52
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.dylib
cmd/diagnosis/diagnosis
cmd/diagnosis/diagnosis.*
cmd/echor/echor

# Test binary, build with `go test -c`
*.test
Expand Down
16 changes: 16 additions & 0 deletions cmd/echor/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"os"

"github.com/wangyoucao577/go-project-layout/stringutil"
)

func main() {
if len(os.Args) > 1 {
fmt.Println(stringutil.Reverse(os.Args[1]))
} else {
fmt.Println() // print empty string
}
}
11 changes: 11 additions & 0 deletions stringutil/reverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package stringutil contains utility functions for working with strings.
package stringutil

// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
20 changes: 20 additions & 0 deletions stringutil/reverse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package stringutil

import "testing"

func TestReverse(t *testing.T) {
cases := []struct {
in, want string
}{
{"Hello, world", "dlrow ,olleH"},
{"Hello, 世界", "界世 ,olleH"},
{"", ""},
}

for _, c := range cases {
got := Reverse(c.in)
if got != c.want {
t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
}
}
}

0 comments on commit d401d52

Please sign in to comment.