Golang for-range Statement
Golang for-range - Golang for-range Statement
The for-range Statement
The fourth for statement format is for iterating over elements in some of Go’s built-in types. It is called a for-range loop and resembles the iterators found in other languages. In this section, we will look at how to use a for-range loop with strings, arrays, slices, and maps. When we cover channels in Chapter 10, we will talk about how to use them with for-range loops.
Note
You can only use a for-range loop to iterate over the built-in compound types and user-defined types that are based on them.
First, let’s take a look at using a for-range loop with a slice. You can try out the code in Example 4-13 on The Go Playground.
Example 4-13. The for-range loop
evenVals := []int{2, 4, 6, 8, 10, 12} for i, v := range evenVals { fmt.Println(i, v) }
Running this code produces the following output:
0 2 1 4 2 6 3 8 4 10 5 12
What makes a for-range loop interesting is that you get two loop variables. The first variable is the position in the data structure being iterated, while the second is the value at that position. The idiomatic names for the two loop variables depend on what is being looped over. When looping over an array, slice, or string, an i for index is commonly used. When iterating through a map, k (for key) is used instead.
The second variable is frequently called v for value, but is sometimes given a name based on the type of the values being iterated. Of course, you can give the variables any names that you like. If there are only a few statements in the body of the loop, single letter variable names work well. For longer (or nested) loops, you’ll want to use more descriptive names.
What if you don’t need to use the key within your for-range loop? Remember, Go requires you to access all declared variables, and this rule applies to the ones declared as part of a for loop, too. If you don’t need to access the key, use an underscore (_) as the variable’s name. This tells Go to ignore the value. Let’s rewrite our slice ranging code to not print out the position. We can run the code in Example 4-14 on The Go Playground.
Example 4-14. Ignoring the key in a for-range loop
evenVals := []int{2, 4, 6, 8, 10, 12} for _, v := range evenVals { fmt.Println(v) }
Running this code produces the following output:
2 4 6 8 10 12
Tip
Any time you are in a situation where there’s a value returned, but you want to ignore it, use an underscore to hide the value. You’ll see the underscore pattern again when we talk about functions in Chapter 5 and packages in Chapter 9.
What if you want the key, but don’t want the value? In this situation, Go allows you to just leave off the second variable. This is valid Go code:
uniqueNames := map[string]bool{“Fred”: true, “Raul”: true, “Wilma”: true} for k := range uniqueNames { fmt.Println(k) }
The most common reason for iterating over the key is when a map is being used as a set. In those situations, the value is unimportant. However, you can also leave off the value when iterating over arrays or slices. This is rare, as the usual reason for iterating over a linear data structure is to access the data. If you find yourself using this format for an array or slice, there’s an excellent chance that you have chosen the wrong data structure and should consider refactoring.
Note
When we look at channels in Chapter 10, we’ll see a situation where a for-range loop only returns a single value each time the loop iterates.