An array in Go has a fixed length that is part of its type, and it is a value: assigning or passing it copies every element. A slice is a small descriptor (pointer, length, capacity) that points into an underlying array, so it can grow with append and copies of a slice share the same data. In everyday Go code you use slices almost everywhere and reach for arrays only when a fixed size is a real requirement.
The difference matters for correctness as well as performance. Mixing up copy semantics (arrays) and shared-memory semantics (slices) is one of the most common sources of surprising bugs for people new to Go.
An array type is written [N]T, where N is a compile-time constant. The length is part of the type, so [3]int and [4]int are different, incompatible types.
Because assignment copies the whole array, changing a does not affect b. Arrays are also comparable with == when their element type is comparable, which makes them usable as map keys.
A slice type is written []T with no length. At runtime a slice value holds three fields: a pointer to the first element it can see, its length, and its capacity (how far it can grow before a new backing array is needed).
The exact capacity after append is an implementation detail of the runtime growth strategy, so never rely on a specific number. A nil slice (var s []int) has length 0, works with append, len and range, and is usually the right empty value.
When you pass an array to a function, the function gets its own copy. When you pass a slice, the function gets a copy of the descriptor, but both descriptors point at the same elements.
The array is unchanged, while the slice shows the modification. Note that append inside a function can still be invisible to the caller: it may return a new descriptor (and possibly a new backing array), so return the result or pass a pointer to the slice.
Slicing an array with arr[low:high] produces a slice that shares the array's memory. The optional third index limits capacity (arr[low:high:max]).
A small slice can keep a very large backing array alive in memory. If you only need a few elements from a big buffer, copy them with slices.Clone or copy so the large array can be garbage collected.
| Aspect | Array [N]T | Slice []T |
|---|---|---|
| Length | Fixed, part of the type | Dynamic, not part of the type |
| Assignment and passing | Copies all elements | Copies a 3-word descriptor, shares elements |
| Zero value | Elements are zero values | nil |
| Growth | Not possible | append |
Comparable with == | Yes, if elements are comparable | No, only against nil (use slices.Equal) |
| Map key | Allowed | Not allowed |
| Typical use | Hashes, fixed buffers, small matrices | Almost everything else |
Choose an array when the size is genuinely fixed and part of the meaning of the data: a SHA-256 digest is [32]byte, an IPv4 address is [4]byte, a chessboard may be [8][8]Piece. Arrays also give you value semantics and comparability for free, and small arrays can live on the stack. For nearly every other case, including function parameters, return values and struct fields that hold lists, use a slice.
No. The length is part of the type and fixed at compile time. If you need a growing collection, use a slice and append.
Not exactly. A slice is a value (the descriptor), but it refers to shared underlying data, so copies behave like references to the same elements. Appending can detach a copy from the original when capacity runs out.
Use slices.Clone(s) (standard library since Go 1.21) or dst := make([]T, len(s)); copy(dst, s).
Not with ==. Use slices.Equal(a, b) for comparable element types, or reflect.DeepEqual for complex cases.
Arrays are fixed-length values that are copied on assignment, and their length is part of their type. Slices are lightweight views over an array that can grow with append and share data when copied. Prefer slices by default, use arrays for fixed-size data such as digests and addresses, and remember to clone a slice when you need independent data.