Easy Tutorial
❮ Go Function Closures Go Slice ❯

Go Language Range

In Go, the range keyword is used within a for loop to iterate over elements in arrays, slices, channels, or maps. For arrays and slices, it returns both the index and the value at that index. For maps, it returns key-value pairs.

The range format in a for loop can iterate over slices, maps, arrays, and strings. Here is the format:

for key, value := range oldMap {
    newMap[key] = value
}

The key and value in the above code can be omitted.

If you only want to read the key, the format is:

for key := range oldMap

Or alternatively:

for key, _ := range oldMap

If you only want to read the value, the format is:

for _, value := range oldMap

Example

Iterating over a simple array, where 2**%d represents the power of two corresponding to the index:

package main

import "fmt"

var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}

func main() {
    for i, v := range pow {
        fmt.Printf("2**%d = %d\n", i, v)
    }
}

The output of the above example is:

2**0 = 1
2**1 = 2
2**2 = 4
2**3 = 8
2**4 = 16
2**5 = 32
2**6 = 64
2**7 = 128

The range format in a for loop can also omit the key and value, as shown in the following example:

package main
import "fmt"

func main() {
    map1 := make(map[int]float32)
    map1[1] = 1.0
    map1[2] = 2.0
    map1[3] = 3.0
    map1[4] = 4.0

    // Reading key and value
    for key, value := range map1 {
        fmt.Printf("key is: %d - value is: %f\n", key, value)
    }

    // Reading key
    for key := range map1 {
        fmt.Printf("key is: %d\n", key)
    }

    // Reading value
    for _, value := range map1 {
        fmt.Printf("value is: %f\n", value)
    }
}

The output of the above example is:

key is: 4 - value is: 4.000000
key is: 1 - value is: 1.000000
key is: 2 - value is: 2.000000
key is: 3 - value is: 3.000000
key is: 1
key is: 2
key is: 3
key is: 4
value is: 1.000000
value is: 2.000000
value is: 3.000000
value is: 4.000000

Example

Iterating over other data structures:

package main
import "fmt"
func main() {
    // This is how we use range to sum a slice. Using an array is very similar.
    nums := []int{2, 3, 4}
    sum := 0
    for _, num := range nums {
        sum += num
    }
    fmt.Println("sum:", sum)
    // Using range on an array will pass both the index and the value. In the previous example, we did not need the index, so we used the blank identifier "_" to omit it. Sometimes, we need to know the index.
    for i, num := range nums {
        if num == 3 {
            fmt.Println("index:", i)
        }
    }
    // Range can also be used on map key-value pairs.
    kvs := map[string]string{"a": "apple", "b": "banana"}
    for k, v := range kvs {
        fmt.Printf("%s -> %s\n", k, v)
    }
}

The range keyword can also be used to iterate over Unicode strings. The first parameter is the index of the character, and the second is the character itself (its Unicode value).

for i, c := range "go" {
    fmt.Println(i, c)
}

The output of the above example is:

sum: 9
index: 1
a -> apple
b -> banana
0 103
1 111
❮ Go Function Closures Go Slice ❯