Arrays

Say your program needs the ratings that three customers gave. You could make three variables, r1, r2 and r3. But with a hundred ratings, that does not work anymore. An array is one variable that holds a fixed number of values of the same type. You reach each value by its position.

package main

import "fmt"

func main() {
	ratings := [3]int{5, 4, 3}
	fmt.Println(ratings)
	fmt.Println(ratings[0])
	fmt.Println(ratings[2])
}

Output:

[5 4 3]
5
3

[3]int is the type. It means "an array of 3 int values". The values in curly braces are the starting values. fmt.Println prints the whole array in square brackets, with a space between the values.

The number in square brackets after the name is the index, the position of a value. Indexes start at 0, not at 1. So the first rating is ratings[0], and in an array of 3 values the last one is ratings[2].

 index:      0   1   2
           +---+---+---+
 ratings:  | 5 | 4 | 3 |
           +---+---+---+

Arrays and loops

Each value in an array is called an element. An element works like a normal variable: you can read it, and you can give it a new value. The built-in function len gives the length of the array, which is the number of elements. With a for loop, you can visit every element:

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

total := 0
for i := 0; i < len(ratings); i++ {
	total += ratings[i]
}
fmt.Println("Total:", total)

Output:

Total: 10

ratings[1] = 2 changes the second element from 4 to 2. Then the loop runs with i equal to 0, 1 and 2, and adds 5 + 2 + 3. The condition i < len(ratings) is better than i < 3, because the loop still works if you change the size later.

Zero values

If you declare an array with var and no starting values, Go fills it with the zero value of its type. For int, that is 0. You can fill it later, for example from input. Here the input is 7 3 9:

var numbers [3]int
fmt.Println(numbers)
for i := 0; i < len(numbers); i++ {
	fmt.Scan(&numbers[i])
}
fmt.Println(numbers)

Output:

[0 0 0]
[7 3 9]

&numbers[i] works like &age in the Reading Input lesson. It tells fmt.Scan where to store the value it reads.

The index must exist

Go checks every index. When the index is a number written in the code, the compiler finds the mistake before the program runs:

package main

import "fmt"

func main() {
	ratings := [3]int{5, 4, 3}
	fmt.Println(ratings[3])
}

Output:

# command-line-arguments
./main.go:7:22: invalid argument: index 3 out of bounds [0:3]

[0:3] means the index must be at least 0 and less than 3. When the index is in a variable, the compiler cannot check it, so Go checks it while the program runs. A bad index stops the program with a panic, which is Go's name for an error at run time:

ratings := [3]int{5, 4, 3}
i := 3
fmt.Println(ratings[i])

Output:

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

The size never changes

The size is part of the type, so [3]int and [4]int are two different types. An array can never grow or shrink. Go also refuses to compile var a [n]int when n is a variable: the size must be a constant.

When you assign an array to another variable, Go copies all the elements:

a := [3]int{5, 4, 3}
b := a
b[0] = 9
fmt.Println(a)
fmt.Println(b)

Output:

[5 4 3]
[9 4 3]

Changing b did not change a. Because of the fixed size, Go programs use arrays only for data whose size never changes, like the 3 parts of a date. For lists that grow, Go has slices, the topic of the next lesson.

In the exercise, read 3 ratings into an array, add them up with a loop, and print the array and the average rating.