Arrays, Slices & Maps
Arrays: fixed size
Section titled “Arrays: fixed size”var nums [3]int // [0, 0, 0]letters := [3]string{"a", "b", "c"}The size is part of the type — [3]int and [4]int are different types. In practice, you’ll rarely use arrays directly; almost everything uses slices instead.
Slices: the workhorse
Section titled “Slices: the workhorse”A slice is a view into an underlying array: a pointer, a length, and a capacity. It grows automatically.
nums := []int{1, 2, 3}nums = append(nums, 4) // [1 2 3 4]
// pre-size for performance when you know the lengthbuf := make([]byte, 0, 1024)Slicing shares the underlying array by default — mutating a slice can mutate the one it was sliced from. Use copy() when you need an independent copy:
dst := make([]int, len(src))copy(dst, src)ages := map[string]int{ "alice": 30, "bob": 25,}
ages["carol"] = 40 // insertage, ok := ages["dave"] // ok is false if the key is missingdelete(ages, "bob")Map iteration order is intentionally randomized by the Go runtime — never rely on it. Sort the keys yourself if you need a deterministic order.