A slice in Go is a window onto an array: the slice value you hold in a variable is a small descriptor, often called the slice header, with three fields: a pointer to the first element of an underlying array, a length that says how many elements the slice currently holds, and a capacity that says how many elements fit before the slice must grow. When you assign a slice or pass it to a function, Go copies only the descriptor, and both copies then point at the same underlying array.
Most slice bugs come from that sharing. When you append to one slice, you overwrite elements another slice is still reading. When you re-slice a large buffer and keep a small part, the whole buffer stays alive in memory. And because the descriptor is copied by value, two goroutines (concurrently running functions) holding the same slice race on the array even when each one only appends.
Copy data when you need independence. Limit capacity with a three-index slice expression (a slice expression that also sets the capacity explicitly). Pre-allocate with make when you know the final size. Treat concurrent append as a race. The sections that follow pair each failure with the code that triggers it and with the pattern that avoids it.
Slices Are Descriptors Over a Shared Array
The official Go slices documentation describes the slice type []T as a descriptor: a pointer to an array, a length, and a capacity. When you write view := data[1:4], Go does not copy the three elements the expression selects. Go builds a new descriptor whose pointer starts at data[1] and whose length is 3, and both descriptors point into the same array.
Because the new slice points into that array, when you write through it, you change the original data:
data := []int{10, 20, 30, 40, 50}
view := data[1:4]
view[0] = 99
fmt.Println(data) // [10 99 30 40 50]
Sharing is a feature when you parse a buffer or walk a tree: slicing is cheap, and views let you avoid copying. It becomes a bug when the caller expects independence. Before you return a sub-slice from a function, decide whether changes to it can leak back into the array you sliced from. If the caller needs independence, copy the data. The built-in copy(dst, src) copies only the smaller of the two lengths, so you size the destination first. The standard library’s slices.Clone copies a slice into a fresh array in one call.
The Append Aliasing Trap
append is the most common cause of slice surprises because its result depends on capacity. When you append to a slice that has spare capacity, the new element lands in the existing underlying array, and any other slice that shares that array sees the write:
a := []int{1, 2, 3, 4}
b := a[:2]
b = append(b, 99)
fmt.Println(a) // [1 2 99 4]
A Stack Overflow question about this exact case shows the same confusion: two slices start from one array, one append call runs, and the first slice has changed. The Go slices documentation states the rule plainly: when the capacity is large enough, append reuses the underlying array; otherwise it allocates a new array and copies the elements over. Code that assumes either outcome without checking the capacity is fragile. A slice expression that extends a slice past its capacity panics at runtime, so append is also the only way to grow a slice safely. Appending to a nil slice works the same as appending to an empty one; the first append call allocates the array.
To fix the aliasing case, use the three-index slice expression s[low:high:max], which sets the capacity of the new slice explicitly. a[:2:2] produces a slice with length 2 and capacity 2. When you append to it, the slice has no room in the shared array, so Go allocates fresh storage:
a := []int{1, 2, 3, 4}
b := a[:2:2]
b = append(b, 99)
fmt.Println(a) // [1 2 3 4]
fmt.Println(b) // [1 2 99]
Both the VictoriaMetrics write-up on slice internals and the analysis of slice resizing recommend the three-index form whenever you append to a slice that came from a larger one. Make it the default for any function that receives a sub-slice and must append to it.
Function Calls and Range Loops
When a function receives a slice, it gets a copy of the descriptor. The function can change elements, and the caller sees those changes, because the descriptor still points at the same array. The function cannot change the caller’s length or capacity, because those live in the descriptor copy. A function that calls append and returns nothing has no way to tell the caller the slice grew. The rundown of common slice mistakes gives the fix directly: return the new slice from the function and assign the result at the call site.
for range loops follow the same rule. The loop variable is a copy of each element, so assigning to it does not change the slice:
for _, v := range s {
v = v * 2 // s is unchanged
}
for i := range s {
s[i] = s[i] * 2 // modifies s in place
}
To modify elements, index into the slice. The same rundown notes that copy has a related trap: with a zero-length destination, it copies zero elements, so you must size the destination before calling it.
Re-Slicing Can Pin Large Arrays in Memory
A slice descriptor keeps its underlying array alive as long as any slice references it. When you re-slice a large buffer and keep only a small part, the rest of the buffer stays in memory. If you read a large file into memory and return a small prefix to a caller, the returned slice still references the array that holds the whole file, and the garbage collector cannot reclaim that array until the slice is gone. The guide to memory-efficient slices shows the fix: copy the part you need into a new slice before returning it, so the large array has no remaining references.
func firstTen(data []byte) []byte {
out := make([]byte, 10)
copy(out, data)
return out
}
copy copies up to the smaller of the two lengths, so it copies at most n bytes, and the garbage collector can reclaim the original buffer. The trade is one small allocation instead of a long-lived large one, which is usually the better choice. The same pattern gives you a sub-slice that can grow independently: once the data is copied, you can append to it without touching the original array.
Pre-Allocate Capacity When You Know the Size
When append runs out of capacity, Go allocates a new array, copies every element over, and discards the old array. A loop that appends one element at a time to an empty slice repeats that copy many times. The runtime commonly doubles capacity while slices are small and changes the growth rate once a slice reaches a few hundred elements, but the exact policy is an implementation detail, not a language guarantee. The VictoriaMetrics analysis and the resizing write-up agree on the same practical rule: when you know the final size, build the slice with make([]T, 0, n) so you can append the first n elements without reallocation. When you only know an upper bound, use that bound as the capacity.
The memory-efficient slices guide recommends the same pre-allocation because each reallocation copies every element accumulated so far; a loop that grows by one element ends up copying the whole prefix many times. You keep the same code that fills the slice: you still append, but Go never needs to grow the slice to fit the new elements.
Slices Under Concurrency
A data race happens when two goroutines access the same memory, at least one access is a write, and no synchronization orders the two accesses. Slices make races easy to create because the descriptor is copied by value while the array is shared: two goroutines can hold descriptors that point at the same array, and nothing in the slice type coordinates them. Uber’s study of data race patterns in Go, based on roughly 1,100 data races fixed by 210 developers over six months in its monorepo, a single repository holding all of Uber’s Go code, found that slices are easy to misuse in concurrent code for exactly this reason. A lock around append does not protect other goroutines that read or write the same underlying array through their own descriptor copies.
Concurrent append calls to the same slice race on the length field of the descriptor, because each append call reads the current length, writes the element, and stores a new length. Build the slice in one goroutine before sharing it, or guard every append call and every read with the same mutex.
Goroutines can safely write to distinct indices of a slice if its underlying array is fully allocated before they start: the Go specification treats each element of an array as a variable in its own right, and the write-up on concurrent slice writes demonstrates the pattern. The pattern breaks if any goroutine also appends or reads the length, so keep index writes separate from append calls, and run the race detector, enabled by go test -race, on the Go version you ship.
Uber’s study also traced a class of read-while-writing bugs to code that calls wg.Add inside a goroutine instead of before the goroutine starts. sync.WaitGroup is a counter that lets the main goroutine wait until the goroutines it launched finish. If code calls Add(1) inside the goroutine, Wait can return before those goroutines finish, so the main goroutine can read the slice while writers still write. Call wg.Add before launching the goroutines.