-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9871515
commit d401d52
Showing
4 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
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
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
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 | ||
} | ||
} |
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
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) | ||
} |
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
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) | ||
} | ||
} | ||
} |