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.

A Go slice is not an array β€” it's a small header (pointer, length, capacity) that views into a backing array. append adds elements; when capacity runs out it allocates a bigger backing array and copies, which is why you always reassign: s = append(s, x).

Slices are the workhorse collection in Go. You rarely use arrays directly. len gives the element count, cap gives how many fit before the next reallocation, and make([]T, len, cap) pre-sizes to avoid repeated growth.

Try it 2 examples

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.