↓ Skip to main content

Arrays in Go

575 words·3 mins· loading · loading ·
Table of Contents
Learn Golang - This article is part of a series.
Part 9: This Article

Updated 16 Nov 2026: rewritten. This version explains that array size is part of the type and that arrays are copied by value, which are the two facts that decide when you use an array rather than a slice.

An array is a numbered row of values of the same type, with a size fixed when you write the code. You’ll use arrays less often than slices, but slices are built on top of them, so it helps to understand arrays first.

Creating arrays
#

Go
var numbers [5]int
fmt.Println(numbers)
[0 0 0 0 0]

The type is [5]int: five integers. An array you declare without values is filled with the zero value of its element type. To give it values, use a literal:

Go
arr := [5]int{10, 20, 30, 40, 50}

Let the compiler count the elements with ...:

Go
days := [...]string{"Mon", "Tue", "Wed"}
fmt.Println(len(days)) // 3

You can also set specific positions and leave the rest as zero values:

Go
sparse := [5]int{1: 10, 3: 30}
fmt.Println(sparse)
[0 10 0 30 0]

The size is part of the type
#

[3]int and [4]int are different types. You can’t assign one to the other, pass one where the other is expected, or compare them. The size is fixed when you write the program, so you can’t grow an array or take its length from a variable.

Indexing
#

Indexes start at 0 and end at len(arr) - 1:

Go
arr := [5]int{1, 2, 3, 4, 5}
fmt.Println(arr[0], arr[2], arr[4]) // 1 3 5

arr[1] = 7
fmt.Println(arr) // [1 7 3 4 5]

Go checks the bounds. A constant index that’s out of range, like arr[5], is a compile error. An out-of-range index computed while the program runs makes it panic:

panic: runtime error: index out of range [5] with length 5

Looping over an array
#

Go
arr := [5]int{1, 2, 3, 4, 5}

for i := 0; i < len(arr); i++ {
	fmt.Println(arr[i])
}

for index, value := range arr {
	fmt.Printf("index %d, value %d\n", index, value)
}

The range form is the one you’ll normally use. It doesn’t need len, and it can’t run past the end.

Arrays are values
#

This is the property that most often surprises people. Assigning an array to another variable copies all of its elements:

Go
a := [3]int{1, 2, 3}
b := a // b is a full copy
b[0] = 99

fmt.Println(a) // [1 2 3]
fmt.Println(b) // [99 2 3]

The same happens when you pass an array to a function: the function gets its own copy, and changing it doesn’t affect the caller. For a large array, that copy is expensive.

Because arrays are plain values, you can compare two arrays of the same type with ==, and it compares every element:

Go
fmt.Println([3]int{1, 2, 3} == [3]int{1, 2, 3}) // true

Multi-dimensional arrays
#

An array can hold other arrays, which is a natural fit for a fixed grid:

Go
var grid [2][3]int

grid[0] = [3]int{1, 2, 3}
grid[1] = [3]int{4, 5, 6}

fmt.Println(grid)
fmt.Println(grid[1][2])
[[1 2 3] [4 5 6]]
6

[2][3]int is two rows of three integers.

When to use an array
#

Reach for an array when the size is genuinely fixed and known in advance, such as a 3-by-3 board, the bytes of a hash ([32]byte for SHA-256), or an IPv4 address ([4]byte). For almost everything else, use a slice, which can grow and is passed without copying its contents. That’s the next post.

Learn Golang - This article is part of a series.
Part 9: This Article

Related