Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

candidosales
Copy link

@candidosales candidosales commented Jul 7, 2020

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant