Created
December 17, 2020 10:18
-
-
Save enihsyou/41744dbda8675ec8aad73920dd6055ac to your computer and use it in GitHub Desktop.
How to iterate through slice in chunk
This file contains 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
// int64 version, iterate through slice in chunk, breaks on first error. | |
func IterateSliceInChunk(slice []int64, chunkSize int, work func([]int64) error) error { | |
if chunkSize < 1 { | |
panic("chunk size is below one") | |
} | |
var offset = 0 | |
var err error | |
for err == nil { | |
lower, upper := offset, offset+chunkSize | |
if upper >= len(slice) { | |
err = work(slice[lower:]) | |
break | |
} else { | |
err = work(slice[lower:upper]) | |
offset = upper | |
} | |
} | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment