tezvyn:

How do you append to a Go slice and why reassign?

AI-drafted, machine-checkedSource: pkg.go.devbeginner

Tests slice headers and append reallocation. A strong answer reassigns the result (s = append(s, 4)), explains that append may allocate a new backing array, and warns that ignoring the return value drops elements. Red flag: calling append without assignment.

WHAT THIS TESTS: This question probes your mental model of Go slices. A slice is not an array; it is a small header struct containing a pointer to a backing array, a length, and a capacity. The interviewer wants to know whether you understand that append is a function that may mutate the backing array in place or may allocate an entirely new backing array, and that it returns an updated slice header which must be used.

A GOOD ANSWER COVERS: First, show the correct syntax: s = append(s, 4). Second, explain that append returns a slice value and that the original variable must be reassigned because the returned slice may point to a different underlying array. Third, describe the two growth paths: if the backing array has enough capacity, append reslices it in place to accommodate the new element; if capacity is exhausted, it allocates a larger array, copies the existing elements, appends the new one, and returns a slice header pointing to that new array. Fourth, note the practical consequence: if you pass a slice to a function that appends without returning the result, or if you ignore the return value in the caller, subsequent code using the original variable will see either a shorter slice or data in a stale array.

COMMON WRONG ANSWERS: The biggest red flag is writing append(s, 4) without capturing the result. Another is claiming that append always modifies the slice in place and that the return value is just a convenience. Some candidates confuse slices with arrays and expect the original variable to update automatically because they think of slices as reference types; while slices contain pointers, the slice header itself is passed by value, so the top-level variable must be overwritten.

LIKELY FOLLOW-UPS: The interviewer may ask what happens to other slices that share the same backing array when append triggers a reallocation. They might ask how to preallocate capacity to avoid allocations, which leads to make([]int, 0, 10) or using append with a known length. They could also ask about the difference between passing a slice to a function by value versus using a pointer to a slice, or how append behaves with nil slices.

ONE CONCRETE EXAMPLE: Imagine you write a := []int{1, 2, 3} and b := a[:2], then you run b = append(b, 99). If the backing array has capacity 3 or more, a becomes [1, 2, 99] because b and a share the array. But if you then append another element to b and it exceeds capacity, b will reallocate and point to a new array, while a still points to the old one. This demonstrates exactly why you must track the returned slice header.

Read the original → pkg.go.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.