typestar

Chunk a slice in Go

Splits a slice into fixed-size pieces using slice expressions.

func chunk(items []int, size int) [][]int {
    var out [][]int
    for size < len(items) {
        out = append(out, items[:size])
        items = items[size:]
    }
    return append(out, items)
}

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
166
Tokens
60
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 40 seconds.

Type this snippet

Step 7 of 9 in Arrays, slices & maps, step 22 of 30 in Language basics.

← Previous Next →