Array — fixed-size, value-type collection where the length is part of the type. Slice — dynamic-length reference to an underlying array, the most common collection in Go.
Arrays are assigned and returned by value, which copies all elements. This makes them safe from unintended sharing but expensive for large sizes.
1arr1 := [3]int{1, 2, 3}2arr2 := arr1 // full copy3arr2[0] = 994fmt.Println(arr1[0]) // 1 — original unchanged
Slices are the standard way to work with sequences in Go. They hold a pointer, length, and capacity. Multiple slices can share the same underlying array, and append may or may not reallocate.
1xs := []int{1, 2, 3, 4, 5}2head := xs[:3] // [1, 2, 3] — shares array3head[0] = 1004fmt.Println(xs[0]) // 100 — shared mutation56xs = append(xs, 6) // may reallocate if cap exhausted
When to use:
Common mistakes:
append returns a new slice header — must reassign: s = append(s, v).== — only valid against nil; use slices.Equal otherwise.