Array — fixed-length, value-type where length is part of the type. Slice — dynamic reference type backed by an array, the standard Go collection.
Arrays have their length baked into the type, so [3]int and [4]int are different types. They are value types — assigning or passing copies all elements.
1var arr [5]int2arr[0] = 423copy := arr // copies all 5 elements4copy[0] = 995fmt.Println(arr[0]) // 42 — unchanged
Slices are views into arrays with pointer, length, and capacity. They share backing memory — multiple slices can reference the same array. append may reallocate when capacity is exceeded.
1nums := []int{1, 2, 3, 4, 5}2first3 := nums[:3] // shares backing array3first3[0] = 1004fmt.Println(nums[0]) // 100 — shared mutation!5nums = append(nums, 6) // may reallocate
When to use:
Common mistakes:
append result — s = append(s, v).== — only valid against nil.