Back to Lo

Core Chunk

docs/data/core-chunk.md

1.53.0433 B
Original Source

Splits a slice into chunks of the given size. The final chunk may be smaller.

go
lo.Chunk([]int{0, 1, 2, 3, 4, 5}, 2)
// [][]int{{0, 1}, {2, 3}, {4, 5}}

lo.Chunk([]int{0, 1, 2, 3, 4, 5, 6}, 2)
// [][]int{{0, 1}, {2, 3}, {4, 5}, {6}}

Note

lo.ChunkString and lo.Chunk functions behave inconsistently for empty input: lo.ChunkString("", n) returns [""] instead of [].

See https://github.com/samber/lo/issues/788