Go

How do arrays differ from slices? Can you convert between them?

Question 47MediumGo 1.22 to 1.25

An array's length is part of its type ([4]int and [5]int are different types). An array is a value: assigning it, passing it, or ranging over it copies every element. Arrays are comparable when their element type is, so they can be map keys (for example map[[2]int]bool for grid coordinates). Slices cannot. Arrays with a constant size can live on the stack with no heap allocation.

a := [3]int{1, 2, 3}
b := a          // full copy
b[0] = 99
fmt.Println(a, a == [3]int{1, 2, 3}) // [1 2 3] true

s := []int{1, 2, 3, 4}

// Slice -> array pointer (Go 1.17): shares memory
p := (*[4]int)(s)
p[0] = 100
fmt.Println(s[0]) // 100

// Slice -> array (Go 1.20): copies
arr := [4]int(s)
arr[1] = 200
fmt.Println(s[1]) // 2

// Panics at runtime if len(s) < array length
// _ = [8]int(s) // panic: cannot convert slice with length 4 to array or pointer to array with length 8

// Array -> slice
sl := a[:] // shares a's storage

Gotcha: a large array parameter such as func f(buf [1<<20]byte) copies 1 MiB on every call. Pass *[N]T or a slice instead.

More on Arrays, Slices, Maps & Strings

All 37 Arrays, Slices, Maps & Strings questions