Arrays vs Slices

A slice is a tiny window (pointer, length, capacity) looking at an array that lives elsewhere.

The idea

An array is a fixed row of lockers: [3]int is exactly 3 lockers, forever, and copying it copies all the contents.

A slice is a see-through frame laid over a row of lockers. The frame stores three things: where it starts (ptr), how many lockers you can see (len) and how many lockers there are from the start to the end of the row (cap). Two frames can sit over the same row, so editing through one is visible through the other. When append runs out of room, Go builds a bigger row, copies everything over and moves the frame to it.

Watch it work

Try it

Common mistakes