Gointrogo1

Slices and append

Go's growable view over an array.

A slice is a length, a capacity, and a pointer into a backing array — append grows it, sometimes by reallocating.

Growing a slice with append

Gointro
0ms
[1 2 3 4 5]len: 5 cap>=5: true[2 4 6 8 10]
Reference · output compiled offline, not runnable in-browser

append extended nums to [1 2 3 4 5] (len 5, with capacity grown to at least 5), and pre-sizing doubled with make([]int, 0, len(nums)) let the loop append all five doubled values without any reallocation.