Skip to content

Instantly share code, notes, and snippets.

@yuchan
Created June 2, 2012 09:20
Show Gist options
  • Save yuchan/2857491 to your computer and use it in GitHub Desktop.
Save yuchan/2857491 to your computer and use it in GitHub Desktop.
Substring #GoLang
//Original
func substr(s string,pos,length int) string{
bytes:=[]byte(s)
l := pos+length
if l > len(bytes) {
l = len(bytes)
}
return string(bytes[pos:l])
}
//Simply
s[pos:length]
//[]byte -> []rune
//Correct version
func substr(s string,pos,length int) string{
runes:=[]rune(s)
l := pos+length
if l > len(runes) {
l = len(runes)
}
return string(runes[pos:l])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment