The built-in make function creates and initialises three kinds of values: slices, maps and channels. Unlike new, it returns a ready-to-use value of the type itself, not a pointer. Go has no dynamic arrays as such; a slice made with make([]T, length, capacity) is the dynamic array you work with.
This guide covers the exact syntax for each type, how length and capacity differ, and when pre-sizing improves performance. Examples target current Go (1.22 and later).
make(T, args) allocates and initialises the internal data structure behind a slice, map or channel. new(T) only allocates zeroed memory and returns a *T.
| Call | Result |
|---|---|
make([]int, 3) | A slice []int of length 3, ready to use |
make(map[string]int) | An empty, usable map |
make(chan int) | An unbuffered channel |
new([]int) | A *[]int pointing to a nil slice |
A nil map cannot be written to; assigning to it panics. That is why maps need make (or a literal like map[string]int{}) before use.
The signature is make([]T, length) or make([]T, length, capacity). The length is the number of elements you can index now; the capacity is how many fit before the runtime must allocate a larger backing array.
All five elements start at the zero value. The length must not exceed the capacity, and both must be non-negative; otherwise make panics (or fails to compile for constant values).
A common mistake is make([]int, 10) followed by append. That produces ten zeros and then your values after them. If you want to append into an empty slice, set the length to zero:
Here range 10 over an integer needs Go 1.22 or later. Because the capacity is known in advance, append never reallocates in this loop.
The form is make(map[K]V) or make(map[K]V, hint). The optional hint is the expected number of entries. It is only a hint, not a limit, and maps grow as needed.
Maps have no cap, only len. Reading a missing key returns the zero value, so use the two-value form to tell a missing key from a stored zero. Maps are not safe for concurrent writes; protect them with a sync.Mutex or use sync.Map for specific access patterns.
make(chan T) creates an unbuffered channel, where a send waits for a receiver. make(chan T, n) creates a buffered channel that holds up to n values before a send blocks.
len(jobs) is the number of queued values and cap(jobs) the buffer size. Only the sender should close a channel.
Without a hint, a slice that grows by append copies its backing array several times, and a map rehashes as it fills. If you know roughly how many items you will store, give that number to make:
Do not guess large numbers. Over-allocating wastes memory, and for small collections the difference is not measurable. Confirm with a benchmark (go test -bench . -benchmem) before tuning.
A slice is a small header (pointer, length, capacity) over a backing array. Slicing an existing slice does not copy data, so writes through one slice can show up in another. make followed by copy gives you an independent slice:
Since Go 1.21 you can also write slices.Clone(a) from the standard slices package, which does the same in one call.
No. A nil slice (var s []int) works with append and len. Use make when you want a specific length or a pre-sized capacity.
No. Arrays have a fixed size known at compile time and structs use literals. make only accepts slice, map and channel types.
The program panics with assignment to entry in nil map. Initialise the map with make or a map literal first.
Similar idea, but the hint does not limit the map and there is no way to read it back. It only reduces early growth work.
Use make for slices, maps and channels, and remember it returns the value itself, not a pointer. Distinguish length from capacity, start with length zero when you plan to append, and pre-size only when you know the expected count and a benchmark shows a benefit.