Arrays are used to hold a fixed number of items of the same type. If you needed to be able to change the number of items held, you would use a 'slice'.
To create an empty array to hold 6 lottery numbers (integers) you could use:
var lottery [6]int
The items in this lottery array start at 0 (zero) and go to 5. To access the first item in our lottery number array, you would use lottery[0] e.g:
fmt.Println(lottery[0])
To get an array's length, use can use the len function:
fmt.Println(len(lottery))
It is good practice to use the array length and not use the number value. This way, if the array is made bigger (e.g. for 7 lottery numbers) or smaller, you do not need to modify your code to be able to handle it.
One of the main advantages of using arrays instead of using multiple variables is that you can use loops to go through arrays.
To fill our lottery array with numbers we will use a for loop (the only loop Go has!). Using 'range' means we want the loop to go from 0 (zero) to one less than the length of the lottery array. It saves typing and is much more readable!
for i := range len(lottery) {
lottery[i] = 1 + rand.Intn(49)
}
To print out our lottery numbers and as we don't need to modify our array, we can use a variation on the above loop. This is usually referred to as a 'for each' type loop. The 'for each' style returns the index and the value from the array, we only care about the value so use '_' to ignore the index.
// Each value is taken from the lottery array and put into the 'v' variable.
for _, v := range lottery {
fmt.Print(v, ",")
}
The Go compiling guide explains the folder set up for a Go program, once that is done:
package main
import (
"fmt"
"math/rand"
)
func main() {
var lottery [6]int
// Put a number in each array box (there may be dupes...)
for i := range len(lottery) {
lottery[i] = 1 + rand.Intn(49)
}
// Show our numbers
for _, v := range lottery {
fmt.Print(v, ",")
}
}
Updated (06/08/2026)