Skip to content

Instantly share code, notes, and snippets.

@elgs
Created August 25, 2014 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elgs/c7fe4c3c83d8c3048f5e to your computer and use it in GitHub Desktop.
Save elgs/c7fe4c3c83d8c3048f5e to your computer and use it in GitHub Desktop.
reverse string in Golang
func reverse(input string) string {
n := 0
rune := make([]rune, len(input))
for _, r := range input {
rune[n] = r
n++
}
rune = rune[0:n]
// Reverse
for i := 0; i < n/2; i++ {
rune[i], rune[n-1-i] = rune[n-1-i], rune[i]
}
// Convert back to UTF-8.
output := string(rune)
return output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment