Skip to content

Instantly share code, notes, and snippets.

@aranw
Created June 19, 2015 22:36
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 aranw/5578b7fbdd6d12c7add9 to your computer and use it in GitHub Desktop.
Save aranw/5578b7fbdd6d12c7add9 to your computer and use it in GitHub Desktop.
package main
import (
"log"
)
func main() {
test := []int{1, 2, 3, 4, 5}
log.Println(test)
chunks := ChunkInts(test, 4)
log.Println(chunks)
}
func ChunkInts(ints []int, chunkSize int) [][]int {
var chunks [][]int
var to, from int
if chunkSize >= len(ints) {
to = len(ints)
} else {
to = chunkSize
}
for from < len(ints) {
if to > len(ints) {
to = len(ints)
}
subArray := ints[from:to]
chunks = append(chunks, subArray)
from = to
to += chunkSize
}
return chunks
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment