Skip to content

Instantly share code, notes, and snippets.

@huantt
Created June 17, 2022 08:33
Show Gist options
  • Select an option

  • Save huantt/4243a2bfd7ce9bfc3051ec295725e728 to your computer and use it in GitHub Desktop.

Select an option

Save huantt/4243a2bfd7ce9bfc3051ec295725e728 to your computer and use it in GitHub Desktop.
func chunkSlice(slice []int, chunkSize int) [][]int {
var chunks [][]int
for i := 0; i < len(slice); i += chunkSize {
end := i + chunkSize
// necessary check to avoid slicing beyond
// slice capacity
if end > len(slice) {
end = len(slice)
}
chunks = append(chunks, slice[i:end])
}
return chunks
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment