The type [n]T is an array of n values of type T.

var x [3]int // [0,0,0]
 

Init with array literal:

var x = [3]int{10, 20, 30}

Init specifying indexes:

var x = [12]int{1, 5: 4, 6, 10: 100, 15} // [1, 0, 0, 0, 0, 4, 6, 0, 0, 0, 100, 15]

Init without specifying the size:

var x = [...]int{10, 20, 30}
// same as
var x = [3]int{1, 2, 3}

An array’s length is part of its type, so arrays cannot be resized.

Array literal:

[3]bool{true, true, false}

Arrays are too rigid to use directly, and slices are more common.

To 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.

You can’t use a type conversion to directly convert arrays of different sizes to identical types.

Convert to slice

Take a slice from an array with a slice expression [:]:

xArray := [4]int{5, 6, 7, 8}
xSlice := xArray[:] // convert the entire array to slice

To convert a subset of an array:

x := [4]int{5, 6, 7, 8}
y := x[:2]
z := x[2:]

Be aware that the memory will be shared, and changes to a slice will be visible on the array as well.

Copy

copy() can be used, same as with slices.

x := []int{1, 2, 3, 4}
d := [4]int{5, 6, 7, 8}
y := make([]int, 2)
copy(y, d[:])
fmt.Println(y) // [5 6]
copy(d[:], x)
fmt.Println(d) // [1 2 3 4]