Go has one array type and one closely related, far more common type: the slice. An array such as [5]int has a fixed length that is part of its type, while a slice such as []int is a flexible view onto an underlying array that can grow with append. In practice you will use slices almost everywhere and arrays only in specific cases.
Arrays also come in several shapes: one-dimensional, multi-dimensional (arrays of arrays), and arrays with an inferred length. This guide explains each, how they behave when copied, and when to choose a slice instead.
An array holds a fixed number of elements of one type. Its length is part of the type, so [3]int and [4]int are different, incompatible types.
Indexing is bounds-checked: an out-of-range constant index is a compile error, and a dynamic one panics at run time. Arrays can be compared with == when their element type is comparable, and an array of comparable elements can be used as a map key.
Assigning an array or passing it to a function copies every element.
This makes large arrays costly to pass around. Pass a pointer (*[N]int) or a slice (x[:]) when you want the function to see or modify the original.
Go has no special matrix type; you nest arrays.
The sizes are fixed at compile time and all rows have the same length. For data whose dimensions are known only at run time, or rows of varying length (jagged), build a slice of slices instead:
A slice is a small descriptor with a pointer to an array, a length and a capacity. Slicing an array or another slice creates a new descriptor over the same memory.
When append exceeds the capacity, Go allocates a larger array and copies the elements, so the result may no longer share memory with the original. Always assign the result back: s = append(s, v). To get an independent copy, use slices.Clone(s) or copy. The slices and maps packages in the standard library (Go 1.21+) provide Sort, Contains, Index, Reverse and more for slices.
| Feature | Array [N]T | Slice []T |
|---|---|---|
| Length | Fixed, part of the type | Dynamic |
| Assignment | Copies all elements | Copies the descriptor (shares data) |
| Zero value | Array of zero values | nil |
| Growth | Not possible | append |
Comparable with == | Yes, if elements are | Only against nil |
| Use as map key | Yes, if elements are comparable | No |
| Typical use | Fixed-size data such as hashes or IP addresses | Almost everything else |
Good uses of arrays are values whose size is part of their meaning: [32]byte for a SHA-256 digest, [4]byte for an IPv4 address, or a small lookup table. Since Go 1.20 you can convert a slice to an array ([4]byte(s)) and to an array pointer.
An array has a fixed length that is part of its type and is copied on assignment. A slice is a dynamically sized view over an array that is passed by descriptor and grows with append.
Slices serve this purpose. append grows them automatically by allocating a larger backing array when needed.
Yes. The whole array is copied. Use a pointer or a slice to avoid the copy.
For fixed sizes, declare var g [3][4]int. For sizes known only at run time, create a slice of slices with make.
Go has fixed-length arrays and flexible slices. Arrays are value types whose length is part of the type, and they can be one- or multi-dimensional. Slices wrap an array with a length and capacity, share memory when re-sliced, and are the default choice. Reach for arrays only when the exact size is meaningful or when you need a comparable, map-key-capable value.