Array — fixed-length, value-type, length is part of the type. Slice — dynamic-length reference type, the standard sequence in Go.
Arrays are copied on assignment and when passed to functions. This makes them predictable but expensive for large sizes. Arrays are comparable with == and can be map keys.
1arr := [3]int{10, 20, 30}2arr2 := arr // full copy3arr2[0] = 994fmt.Println(arr) // [10 20 30] — unchanged5// Arrays can be map keys6lookup := map[[32]byte]string{sha256.Sum256(data): "found"}
Slices are views into arrays. They share backing memory, so modifications through one slice affect others. append may reallocate if capacity is exceeded.
1data := []int{1, 2, 3, 4, 5}2mid := data[1:4] // [2, 3, 4] — shares backing array3mid[0] = 204fmt.Println(data[1]) // 20 — shared mutation5data = append(data, 6) // may allocate new array
When to use:
Common mistakes:
append return value — append(s, v) returns a new header.== — only valid against nil; use slices.Equal.