Created
December 20, 2014 04:50
-
-
Save hogedigo/100a1e978f887eff6ceb to your computer and use it in GitHub Desktop.
マルチバイトを考慮したreverse
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
package tool | |
import ( | |
"bytes" | |
) | |
func Reverse(s string) string { | |
runes := make([]rune, 0, 10) | |
for _, r := range s { | |
runes = append(runes, r) | |
} | |
var buf bytes.Buffer | |
for i := len(runes) - 1; i >= 0; i-- { | |
if _, err := buf.WriteRune(runes[i]); err != nil { | |
return "error" | |
} | |
} | |
return buf.String() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment