liveBook · Manning

Slice length

As explained in 100 Go Mistakes and How to Avoid Them

Slice length is the number of elements currently contained in a Go slice and available through its indices.

A slice’s length is the number of elements it contains. Its indices run from 0 to len(s)-1. Capacity describes the space available in the backing array; it does not make elements beyond the current length directly indexable. section 3.4

Allocated does not mean indexable

s := make([]int, 3, 6) s[1] = 1 // s[4] = 0 // panics: index 4 is outside length 3 s = append(s, 2) // s is [0 1 0 2], with length 4 The assignment at index 1 changes an existing element, not the length. Append uses available capacity and returns a slice with an increased length. section 3.4

Slices can share a backing array while having different lengths and capacities. Changes to a shared element are visible through both slices; appending can also affect shared storage. A copy or a full slice expression can prevent append conflicts, but only a copy prevents a small slice from retaining a large backing array. section 3.4 section 3.14

Figure 3.6 The slices s1 and s2 reference the same backing array with different lengths and capacities.

The two slices share an array but have different lengths and capacities. section 3.4

Choose length or capacity when initializing

// Known result size: initialize elements, then assign. bars := make([]Bar, len(foos)) for i, foo := range foos { bars[i] = fooToBar(foo) } // Alternatively, reserve space and append. bars := make([]Bar, 0, len(foos)) for _, foo := range foos { bars = append(bars, fooToBar(foo)) } Use one approach or the other. Appending to the first slice would add elements after its already initialized entries. Reserving capacity is also useful when the final count may be smaller than the estimate. section 3.5

Copy needs destination length

src := []int{0, 1, 2} var dst []int copy(dst, src) // copies 0 elements dst = make([]int, len(src)) copy(dst, src) // copies 3 elements copy takes the destination first and copies the minimum of the source and destination lengths. Capacity alone is not enough. section 3.8

To test whether a slice contains no elements, use len(s) == 0. This works for both nil and empty slices, even though they can behave differently with encoding/json or reflect. section 3.14

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains the slice descriptor and backing array, then traces indexing, append, slicing, allocation, and copy behavior.
Examples
Compares slice initialization strategies and demonstrates an out-of-range index and a zero-length copy destination.
What you can do after
Diagnoses slice indexing and copy mistakes and chooses length or capacity when initializing slices.
How it connects
Connects slice length with capacity, backing arrays, append, copying, allocation, and nil slices.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.