Skip to content

Instantly share code, notes, and snippets.

@itang
Last active May 26, 2018 10:06
Show Gist options
  • Save itang/8109481 to your computer and use it in GitHub Desktop.
Save itang/8109481 to your computer and use it in GitHub Desktop.
golang: reverse string
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
fmt.Println(reverse("abc"))
fmt.Println(reverse("中文"))
}
func reverse(s string) string {
cs := make([]rune, utf8.RuneCountInString(s))
i := len(cs)
for _, c := range s {
i--
cs[i] = c
}
return string(cs)
}
@itang
Copy link
Author

itang commented Dec 24, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment