juniorGolang
How do arrays differ from slices in Go?
Updated Apr 28, 2026
Short answer
Arrays have a fixed size defined at compile time. Slices are dynamic, flexible views into the elements of an array.
Deep explanation
An array is a value type; assigning it copies all its elements. A slice is a descriptor containing a pointer to the underlying array, the length of the slice, and the capacity. When a slice exceeds its capacity during an append, Go allocates a new, larger array under the hood and copies the elements over.
Real-world example
Using slices to build dynamic lists of user records fetched from a database where the total count is unknown ahead of time.
Common mistakes
- Passing an array to a function expecting it to be modified. It's passed by value (copied). Use slices (or pointers to arrays) to modify the underlying data.
Follow-up questions
- What happens when you slice a slice?