Internal Structure Of A Go Slice
Fast recall
Quick revision
Core answer
Practical understanding of Slice Internals Len Cap.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
At the machine level a slice is a small struct — pointer, length, capacity — passed around by value. The elements live elsewhere, in a separate array the header points into. That's the whole trick behind slice behavior.
When you assign or pass a slice, those three words get copied. The new header has the same pointer, so both slices view the same elements — a write through one is visible through the other. When you append and there's capacity spare, the write goes into that shared array, and any slice with a window over the slot sees it.
This is why slice behavior feels inconsistent to newcomers: assignment shares, but growth forks. It's not inconsistent — it's a pointer-with-bounds that grows by getting a new pointer. Every surprise is one of those two facts surfacing.
Internal Structure Of A Go Slice
Direct Answer
Three words: a pointer to the backing array's start, the length, and the capacity. Copying a slice copies this header — the elements stay shared.
Deep Dive & Architecture
You can watch the header be three words with unsafe or with the reflect package, but the behavior demonstrates it more usefully:
s := []int{1, 2, 3}
t := s // header copied — same backing array
t[0] = 100
fmt.Println(s[0]) // 100 — shared storage
u := append(s, 4) // s is at cap (3/3): must reallocate
s[0] = 200
fmt.Println(u[0]) // 100 — u forked to new storage
The first print shows sharing (header copy, same pointer). The second shows forking: append at capacity allocated a new array, copied the elements, and returned a header pointing at the new one. s's header still points at the old array, so s[0] = 200 doesn't touch u.
This is also the mechanism behind Q1's len/cap facts and every 'why did my slice change' bug in Go — they're all header semantics. The array itself never moves until an append forces it; only headers are copied, and only growth reallocates.
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is Slice Internals Len Cap, and when would you use it?
- How would you compare Slice Internals Len Cap with an alternative?
- What is a common mistake when using Slice Internals Len Cap?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.