You now have the knowledge on how to iterate a slice, or a map, in Go.
As a recap, you learnt how every iteration returns two values: the index/key and a copy of the element at that index/key.
Easy as pie, loops over a slice, ordered as expected.
xi := []int{10, 20, 30}
for i, x := range xi {
fmt.Println(i, x)
}
// outputs:
// 0, 10
// 1, 20
// 2, 30
Iterating over a map raises a new problem. The order is now random.
hash := map[int]int{9: 10, 99: 20, 999: 30}
for k, v := range hash {
fmt.Println(k, v)
}
// outputs, for example:
// 99 20
// 999 30
// 9 10
It may seem the above output is incorrect, as one would expect the first key/value pair on the declaration of the map 9 10
to be the first one printed and not the last.
However, maps are unordered by nature - there isn't a first or last key/value pair.
Because of that, when iterating over the entries of a map, the order by which entries will be visited will be random and not follow any specific pattern.
This means the above output is possible but might differ from what you get if you try to run this yourself.
To learn more about this see Go Language Spec: range clause.
In Go an unused variable will raise an error at build time. Sometimes you only need the value, as per the first example:
xi := []int{10, 20, 30}
for i, x := range xi {
fmt.Println(x)
}
// Go build failed: i declared but not used
You can replace the i
with _
which tells the compiler we don't use that value:
xi := []int{10, 20, 30}
for _, x := range xi {
fmt.Println(x)
}
// outputs:
// 10
// 20
// 30
If you want to only print the index, you can replace the x
with _
,
or simply omit the declaration:
xi := []int{10, 20, 30}
// for i, _ := range xi {
for i := range xi {
fmt.Println(i)
}
// outputs:
// 0
// 1
// 2
Last but not least, if you are required to perform some action but you are not interested in values nor keys of the slice or map, you can omit both index and value:
xi := []int{10, 20, 30}
count := 0
for range xi {
count++
}
// count value:
// 3