Indexing an array
A Go array stores its elements in order, and every element has a numbered position called its index. To read an element, write the array's name followed by the index in square brackets:
animals := [3]string{"cat", "dog", "parrot"}
fmt.Println(animals[0])
fmt.Println(animals[1])
fmt.Println(animals[2])
cat
dog
parrot
Go uses zero-based indexing. The first element is at index 0, the second at index 1, and so on. An array of three elements has the valid indices 0, 1 and 2, and an array of n elements has the indices 0 to n - 1.
The expression animals[1] is a normal value of the element type, here a string. You can print it, store it in a variable, pass it to a function or use it in a larger expression.
Using elements in expressions
Because an element of an [4]int is a plain int, you can do arithmetic on it directly:
ages := [4]int{14, 32, 50, 21}
total := ages[0] + ages[1]
fmt.Println(total)
fmt.Println(ages[2] - ages[3])
46
29
The whole expression is worked out first, and fmt.Println receives only the result. You do not need a temporary variable unless it makes the code easier to read.
Indexing with a variable
The index does not have to be a literal number. Any integer expression works, so a variable can pick the element:
i := 2
fmt.Println(ages[i]) // 50
This is what makes arrays useful in loops: the same line of code reads a different element each time i changes. When the index is a constant, such as ages[4] on a four-element array, the compiler can check it before the program runs.
The last element
For an array with four elements, the last index is 3. When you do not want to count by hand, use the built-in len, which returns the number of elements. The last index is always len(array) - 1:
fmt.Println(ages[len(ages)-1]) // 21
Go has no negative indices. Writing ages[-1] with a constant index is not a way to reach the end, as it is in some other languages. The compiler rejects it with the message index -1 (constant of type int) must not be negative.
The exercise
The lesson starts with two arrays:
fruits := [3]string{"mango", "kiwi", "grapefruit"}
ages := [4]int{14, 32, 50, 21}
It asks for three printed results.
- The second fruit. The second item has index 1, not 2, so the call is
fmt.Println(fruits[1]). It printskiwi. - The sum of the first three ages. These sit at indices 0, 1 and 2, so add
ages[0] + ages[1] + ages[2]inside the call. The result is 14 + 32 + 50, which is96. - The last age minus the first. The last one is at index 3, so the call is
ages[3] - ages[0], which is 21 - 14, so it prints7.
fmt.Println(fruits[1])
fmt.Println(ages[0] + ages[1] + ages[2])
fmt.Println(ages[3] - ages[0])
kiwi
96
7
Each fmt.Println ends its output with a new line, so the three results appear on separate lines.
Common mistakes
- Counting from 1. The "second" element is at index 1. Being off by one is the most common array bug.
- Using the length as an index. For four elements,
ages[4]is one past the end. The last valid index is 3. - Mixing up types.
fruits[1] + ages[1]does not compile, because Go will not add a string and an int. Each element has the type the array was declared with. - Using a string as an index. Arrays are indexed by integers only. If you want to look something up by a name, use a map instead.
Key takeaways
- Read an element with
array[index]; indices start at 0. - An array with
nelements has valid indices 0 ton - 1, and the last one islen(array) - 1. - An element is an ordinary value of the array's element type, so you can use it in arithmetic and calls.
- Go has no negative indices.