ReverseString
Reverses a string
- Use
make()to create an appropriateruneslice. - Use
rangeandlen()to iterate over the string’s runes and add them in reverse to the result. - Use
string()to convert theruneslice to astring.
func ReverseString(s string) string {
o := make([]rune, len(s))
for i, c := range s {
o[len(s)-i-1] = c
}
return string(o)
}ReverseString("hello") // "olleh"