Go Range project source code

Go Range, Iterating Over Maps and Slices

So far when we’ve introduced Maps, Arrays, and Slices we’ve only talked about how to retrieve a single value from them at a time. While this is useful when we know a specific value to retrieve, it doesn’t help us if we want to do something with all of the values. In programming, the process of looping through all of the values and taking some action is called iteration. We can use the Go Range statement combined with a for loop to iterate over Maps, Arrays, and Slices.

The Go Range statement returns one element of the iterable variable, which when used with the for loop allows us to loop over all the elements of the variable performing some action use them. Each execution of the Go Range statement within the loop returns two values. When used with a map it returns the key and the value, in that order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main

import (
    "fmt"
)

func main () {
    myMap := map[string]string{
        "one": "foo",
        "two": "bar",
        "three": "baz",
    }

    for key, value := range myMap {
        fmt.Println("key", key, "contains value", value)
    }
}

Go supports assigning values to multiple variables in the same statement. Because the Go Range statement returns two values we must have two variables on the left side of our assignment to store those two values it returns. When we use the Go Range statement with an array or slice it again returns two values, this time it’s the index position of the array or slice and the value contained in that position.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main

import (
    "fmt"
)

func main () {
    mySlice := []string{
        "foo",
        "bar",
        "baz",
    }

    for index, value := range mySlice {
        fmt.Println("index", index, "contains value", value)
    }
}

As I mentioned, because the Go Range statement returns two values you must have two variables to hold those values. What if you don’t need both values though? For example, if you want the value of an array position and don’t need to use the index. In Go you cannot create variables that you don’t use, this will throw an error, and your code will not compile. To get around this, you can use the underscore (_) as a placeholder to ignore values that you don’t need. The underscore can be used in any assignment operation, not just for the return values of the Go Range statement.

1
2
3
for _, value := range mySlice {
        fmt.Println("value", value)
}

You’ll only need to use the underscore if you want to ignore the first value returned by the Go Range statement. When a statement returns multiple values, you can ignore the last value by merely not assigning it to a variable.

1
2
3
for index := range mySlice {
        fmt.Println("index", index)
}

Leaving off the second variable is functionally the same as ignoring it using the underscore but makes your code less cluttered.

Chris Sotherden

Originally a native of upstate NY, I relocated to North Carolina in 2013 where I've since helped open the new R&D headquarters for Optanix. I've been with Optanix (formerly ShoreGroup) for over ten years, where I've worked my way up from entry-level to Lead Architect. I taught myself programming in high school and am an all around technology nerd from the design of programming languages and compilers, to quantum computing, and rockets. Outside of technology I've competed in powerlifting and medium distance running, as well as smoking some mean BBQ. Oh, my partner and I also breed, show, and sell fancy goldfish!

>