Skip to content

Instantly share code, notes, and snippets.

@nanmu42
Created March 8, 2021 08:10
Show Gist options
  • Save nanmu42/caeb45bb10a769ca5fc29c28cfcab843 to your computer and use it in GitHub Desktop.
Save nanmu42/caeb45bb10a769ca5fc29c28cfcab843 to your computer and use it in GitHub Desktop.
Golang: returns first n UTF-8 character at Most
func firstNUTF8AtMost(s string, n int) string {
i := 0
for j := range s {
if i >= n {
return s[:j]
}
i++
}
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment