I'm trying and failing to accomplish the simple task of batching over input, at most 10 at a time. The follow code almost works:
func batchMe(input []int) {
fmt.Println("Length", len(input), len(input)/10)
for i := 0; i <= len(input)/10; i++ {
from := i * 10
to := (i + 1) * 10
if len(input) < to {
to = len(input)
}
fmt.Println("Batch", i, input[from:to])
}
But you can see from https://play.golang.org/p/_UgFD1iDyse that it prints:
Length 10 1
Batch 0 [1 2 3 4 5 6 7 8 9 10]
Batch 1 []
I don't want it to print Batch 1 in the case of 10 elements!
Perhaps there is a code simplification here?
Elegant solution from Tv on #go-nuts looks like:
for len(input) > 0 {
n := 10
if n > len(input) {
n = len(input)
}
chunk := input[:n]
input = input[n:]
fmt.Println("Batch", chunk)
}