A slice is a growable view over an array, while an array has a fixed size. An array's size is part of its type, so [5]int and [6]int are different types. A slice is a small header of three fields: a pointer to the array, a length and a capacity.
Arrays are values, so assigning one copies every element. Assigning a slice copies only its header, so both slices share the same elements.
// Array - fixed size
var arr [5]int = [5]int{1, 2, 3, 4, 5}
// Slice - dynamic size
var slice []int = []int{1, 2, 3, 4, 5}
slice = append(slice, 6) // Can grow
// Creating slices
slice1 := make([]int, 5) // length 5, capacity 5
slice2 := make([]int, 5, 10) // length 5, capacity 10
Most Go code uses slices, because they grow with built-in functions like append() and pass cheaply to functions.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓