Array — fixed-length, value-type, length is part of the type definition. Slice — dynamic reference type backed by an array, the standard Go collection.
Arrays are rarely used directly because their fixed size is restrictive. They're mainly useful as building blocks for slices and as map keys (since arrays are comparable).
1arr := [3]int{1, 2, 3}2// Can be map keys3lookup := map[[32]byte]string{4 sha256.Sum256([]byte("hello")): "greeting",5}
Slices wrap arrays with a pointer, length, and capacity. They share backing memory — mutations through one slice affect others referencing the same array.
1data := []int{10, 20, 30, 40}2sub := data[1:3] // [20, 30] — shares backing array3sub[0] = 200 // mutates data[1] too4fmt.Println(data[1]) // 20056// append may or may not reallocate7data = append(data, 50)
When to use:
Common mistakes:
append return value.== — only valid against nil.