02 / 05

What is the internal structure of a slice? How does append work?

A slice is a three-field struct — pointer to underlying array, length, and capacity. Append reuses the existing array if capacity allows, otherwise allocates a new larger array.

Slice internals and append behavior
Key points for interviews
  1. 1

    Append always returns the new slice — the original slice header is not modified

  2. 2

    Sub-slices share the underlying array — mutations are visible across all sub-slices

  3. 3

    Use copy() to create an independent slice when you need isolation

  4. 4

    Pre-allocate with make([]T, 0, expectedSize) to avoid repeated reallocations in loops

  5. 5

    Growth factor is approximately 2x for small slices, decreasing for very large slices