Knaph

Composite Types34 of 62

Preallocate a Slice

The starter code reads n integers into values. Build a second slice, result, using make([]int, 0, n) — length 0, but capacity n — then append every element of values to it. Print result with fmt.Println, then print cap(result) on its own line. Because you asked for capacity n up front, append never has to grow the backing array, so the capacity stays exactly n.

Sign in to try this exercise.