batch
batch copied to clipboard
[BUG] Does not return the last item in the batch list
The error occurs when:
- You pass just one item and it returns null or
- When you list elements and never stop at the last item.
Example: https://play.golang.org/p/R8TgDSz40Ow
Example corrected: https://play.golang.org/p/y7eBkdUhTDj
New implementation:
func batch(count, batchSize int, eachFn BatchFunc) error {
for i := 0; i < count; i += batchSize {
j := i + batchSize
if j > count {
j = count
}
err := eachFn(i, j)
if err == errors.New("done") {
return nil
}
if err != nil {
return err
}
}
return nil
}