Created
June 19, 2015 22:36
-
-
Save aranw/5578b7fbdd6d12c7add9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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