Like most programming languages, Go has arrays. However, arrays are rarely used directly in Go. You’ll learn why in a bit, but first let’s quickly cover array declaration syntax and use.
All elements in the array must be of the type that’s specified (this is unsurprisingly called the element type). There are a few declaration styles. In the first, you specify the size of the array and the type of the elements in the array:
var x [3]int
This creates an array of three ints. Since no values were specified, all of the elements (x[0], x[1], and x[2]) are initialized to the zero value for an int, which is (of course) 0. If you have initial values for the array, you specify them with an array literal:
var x = [3]int{10, 20, 30}
If you have a sparse array (an array where most elements are set to their zero value), you can specify only the indices with nonzero values in the array literal:
This creates an array of 12 ints with the following values: [1, 0, 0, 0, 0, 4, 6, 0, 0, 0, 100, 15].
When using an array literal to initialize an array, you can replace the number that specifies the number of elements in the array with...:
var x = [...]int{10, 20, 30}
You can use == and != to compare two arrays. Arrays are equal if they are the same length and contain equal values:
var x = [...]int{1, 2, 3}
var y = [3]int{1, 2, 3}
fmt.Println(x == y) // prints true
Go has only one-dimensional arrays, but you can simulate multidimensional arrays:
var x [2][3]int
This declares x to be an array of length 2 whose type is an array of ints of length 3. This sounds pedantic, but some languages have true matrix support, like Fortran or Julia; Go isn’t one of them.
Like most languages, arrays in Go are read and written using bracket syntax:
x[0] = 10
fmt.Println(x[2])
You cannot read or write past the end of an array or use a negative index. If you do this with a constant or literal index, it is a compile-time error. An out-of-bounds read or write with a variable index compiles but fails at runtime with a panic (you’ll learn more about panics in the Panic Recover section).
Finally, the built-in function len takes in an array and returns its length:
fmt.Println(len(x))
Earlier I said that arrays in Go are rarely used explicitly. This is because they come with an unusual limitation: Go considers the size of the array to be part of the type of the array. This makes an array that’s declared to be [3]int a different type from an array that’s declared to be [4]int. This also means that you cannot use a variable to specify the size of an array, because types must be resolved at compile time, not at runtime.
What’s more, you can’t use a type conversion to directly convert arrays of different sizes to identical types. Because you can’t convert arrays of different sizes into each other, you can’t write a function that works with arrays of any size and you can’t assign arrays of different sizes to the same variable.
Note
You’ll learn how arrays work behind the scenes when I discuss memory layout in the related discussion.
Because of these restrictions, don’t use arrays unless you know the exact length you need ahead of time. For example, some of the cryptographic functions in the standard library return arrays because the sizes of checksums are defined as part of the algorithm. This is the exception, not the rule.
This raises the question: why is such a limited feature in the language? The main reason arrays exist in Go is to provide the backing store for slices, which are one of the most useful features of Go.