Skip to content

Instantly share code, notes, and snippets.

@denniswebb
Created December 5, 2017 19:31
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 denniswebb/14e7da27ab0a78099731b45e41ec0120 to your computer and use it in GitHub Desktop.
Save denniswebb/14e7da27ab0a78099731b45e41ec0120 to your computer and use it in GitHub Desktop.
Golang function to split a string by size on a separator.
func StringSplitAfterNSize(s, sep string, size int) (resp []string) {
rl := ""
for _, line := range strings.SplitAfter(s, sep) {
if len(rl+line) > size {
resp = append(resp, rl)
rl = ""
}
rl += line
}
if len(resp) > 0 && len(rl) == 0 {
return
}
resp = append(resp, rl)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment