Go Reference ยท Built-in Types
Go arrays
Reference for Go arrays: fixed length as part of the type, value semantics on assignment and calls, comparison, [...] literals, multi-dimensional arrays.
An array is a numbered sequence of elements of one type whose length is fixed at compile time. The length is part of the type: [3]int and [4]int are different, incompatible types. Arrays are values. Assigning one to another variable, or passing it to a function, copies every element.
Most Go code uses slices rather than arrays. Arrays are still useful as keys in maps, as fixed-size buffers, for hashes such as [32]byte, and as the backing storage behind slices.
var scores [3]int // [0 0 0] primes := [...]int{2, 3, 5} // length inferred: [3]int
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
var a [N]T // N is a constant expression >= 0 a := [N]T{v0, v1, ...} // missing elements are zero a := [...]T{v0, v1, v2} // compiler counts the elements a := [...]T{2: "x", 5: "y"} // keyed: length is the highest index + 1
| Part | Description |
|---|---|
N |
A non-negative constant of integer type that the compiler can represent as int. Not a variable. |
T |
Any element type, including other arrays (for grids) and structs. |
len(a), cap(a) |
Both equal N, and both are constants when a has no function calls or channel receives inside it. |
Edge cases: indexing out of range panics at run time; a constant index outside 0..N-1 is a compile error. Writing a literal with more elements than N is also a compile error.
Usage notes
- The length is in the type. A function taking
[3]intcannot be called with a[4]int. Take a slice ([]int) for variable lengths. - Copies are real. Passing a large array by value copies all of it; pass a pointer (
*[N]T) or a slice instead. Indexing,lenandrangework directly through an array pointer. - Comparable if the element type is comparable.
==compares all elements, so arrays can be map keys, which slices cannot. - Slicing an array (
a[:]) produces a slice sharing the array's memory. The array must be addressable, so slice a variable, not a function result. rangeover an array copies it when you use the value variable; range over&aor use only the index to avoid that copy.- Zero value: every element is its zero value, with no allocation step needed.
- Since Go 1.20 you can convert a slice to an array (
[3]int(s)) or array pointer; it panics if the slice is shorter than the array.
Related
- Slices, Maps, len(), Zero values
- Lessons: Arrays, Slices, Slice internals