Introduction
The range keyword is used to iterate over elements in a variety of data structures in Golang. In this lab, we will explore how to use range with different data structures.
Range
The problem to be solved in this lab is to demonstrate how to use range with slices, arrays, maps, and strings.
To complete this lab, you will need:
- Basic knowledge of Golang syntax
- Golang installed on your machine
$ go run range.go
sum: 9
index: 1
a - > apple
b - > banana
key: a
key: b
0 103
1 111
There is the full code below:
// _range_ iterates over elements in a variety of data
// structures. Let's see how to use `range` with some
// of the data structures we've already learned.
package main
import "fmt"
func main() {
// Here we use `range` to sum the numbers in a slice.
// Arrays work like this too.
nums := []int{2, 3, 4}
sum := 0
for _, num := range nums {
sum += num
}
fmt.Println("sum:", sum)
// `range` on arrays and slices provides both the
// index and value for each entry. Above we didn't
// need the index, so we ignored it with the
// blank identifier `_`. Sometimes we actually want
// the indexes though.
for i, num := range nums {
if num == 3 {
fmt.Println("index:", i)
}
}
// `range` on map iterates over key/value pairs.
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {
fmt.Printf("%s -> %s\n", k, v)
}
// `range` can also iterate over just the keys of a map.
for k := range kvs {
fmt.Println("key:", k)
}
// `range` on strings iterates over Unicode code
// points. The first value is the starting byte index
// of the `rune` and the second the `rune` itself.
// See [Strings and Runes](strings-and-runes) for more
// details.
for i, c := range "go" {
fmt.Println(i, c)
}
}
Summary
In this lab, we learned how to use range with slices, arrays, maps, and strings in Golang. The range keyword provides a convenient way to iterate over elements in different data structures.