batch icon indicating copy to clipboard operation
batch copied to clipboard

[BUG] Does not return the last item in the batch list

Open candidosales opened this issue 4 years ago • 0 comments

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
}

candidosales avatar Jul 07 '20 05:07 candidosales