Skip to the workspace
Pebellslearn by building
Arrays & Slices  ›  Lesson

make()

make() 12 minmake builds a slice with a chosen length and capacity (and also initialises maps and channels), and the length you give is filled with zero values, so appending adds after them.

golang / make()

Explore each scene at your own pace.

01 · The rule

The rule

make()

make builds a slice with a chosen length and capacity (and also initialises maps and channels), and the length you give is filled with zero values, so appending adds after them.

Distinguish allocating usable slices, maps and channels from obtaining a zero value.

tally often knows roughly how many words a file holds before it starts. Growing a slice one reallocation at a time works, but telling Go the size up front is faster and clearer. make does that — as long as you remember that length and capacity are different promises.

The rule

make([]T, len, cap): len is filled with zeros, cap is room to grow.

a := make([]int, 3)       // [0 0 0], len 3, cap 3
b := make([]int, 0, 1000) // [],      len 0, cap 1000
c := make([]string, 2, 5) // ["" ""], len 2, cap 5
  • The length elements exist right away and hold zero values. You can index them: a[2] = 7.
  • The capacity is extra room behind them. append uses it without reallocating until it is full.
  • Leave capacity out and it equals the length.

That gives two clean patterns:

// Pattern 1: unknown exact count, known rough size — append.
words := make([]string, 0, 1000)
for _, w := range incoming {
	words = append(words, w)
}

// Pattern 2: exact count known — assign by index.
lengths := make([]int, len(words))
for i, w := range words {
	lengths[i] = len(w)
}

make also initialises maps — make(map[string]int) — and channels. It does not work for arrays or structs; those are ready as plain values. (You will also meet new(T), which returns a pointer to a zero T; it is not the tool for slices.)

A slice literal []string{} or a nil slice is fine when you have no size hint at all.

Why bother with a capacity hint? Each time append runs out of room it allocates a bigger array and copies everything across. For a thousand words that is a handful of wasted copies; for millions of rows it is real time. A good hint makes it a single allocation.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.