Range in Go
In Go, the range keyword is used in for loops to iterate over the elements of an array, slice, channel, or map. In arrays and slices it returns the index of the element and the value corresponding to the index; in maps it returns key-value pairs.
The range form of a for loop can iterate over slices, maps, arrays, strings, etc. The format is as follows:
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 as follows:
for key := range oldMap
Or like this:
for key, _ := range oldMap
If you only want to read the value, the format is as follows:
for _, value := range oldMap
Example
Arrays and slices
Iterate over a simple slice; the result of 2**%d is the corresponding power of 2:
Example
package
main
import
"fmt"
// declare a slice containing powers of 2
var
pow
=
[]
int
{
1
,
2
,
4
,
8
,
16
,
32
,
64
,
128
}
func
main
()
{
// iterate over the pow slice, i is the index, v is the value
for
i
,
v
:=
range
pow
{
// print that 2 to the power of i equals v
fmt
.
Printf
(
"2**%d = %d\n"
,
i
,
v
)
}
}
The above example produces the following output:
2**0 = 1
2**1 = 2
2**2 = 4
2**3 = 8
2**4 = 16
2**5 = 32
2**6 = 64
2**7 = 128
String
When range iterates over a string, it returns the index and Unicode code point (rune) of each character.
Example
package
main
import
"fmt"
func
main
()
{
for
i
,
c
:=
range
"hello"
{
fmt
.
Printf
(
"index: %d, char: %c\n"
,
i
,
c
)
}
}
The output of running the above example is:
index: 0, char: h
index: 1, char: e
index: 2, char: l
index: 3, char: l
index: 4, char: o
Map
The range form of a for loop can omit key and value, as in the following example:
Example
package
main
import
"fmt"
func
main
()
{
// create an empty map with int keys and float32 values
map1
:=
make
(
map
[
int
]
float32
)
// add key-value pairs to map1
map1
[
1
]
=
1
.
0
map1
[
2
]
=
2
.
0
map1
[
3
]
=
3
.
0
map1
[
4
]
=
4
.
0
// iterate over map1, reading key and value
for
key
,
value
:=
range
map1
{
// print key and value
fmt
.
Printf
(
"key is: %d - value is: %f\n"
,
key
,
value
)
}
// iterate over map1, reading only the key
for
key
:=
range
map1
{
// print key
fmt
.
Printf
(
"key is: %d\n"
,
key
)
}
// iterate over map1, reading only the value
for
_
,
value
:=
range
map1
{
// print value
fmt
.
Printf
(
"value is: %f\n"
,
value
)
}
}
The output of running 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
Channel
range iterates over values received from a channel until the channel is closed.
Example
package
main
import
"fmt"
func
main
()
{
ch
:=
make
(
chan
int
,
2
)
ch <
1
ch <
2
close
(
ch
)
for
v
:=
range
ch
{
fmt
.
Println
(
v
)
}
}
The output of running the above example is:
1
2
Ignoring values
You can use _ during iteration to ignore the index or value.
Example
package
main
import
"fmt"
func
main
()
{
nums
:=
[]
int
{
2
,
3
,
4
}
// ignore the index
for
_
,
num
:=
range
nums
{
fmt
.
Println
(
"value:"
,
num
)
}
// ignore the value
for
i
:=
range
nums
{
fmt
.
Println
(
"index:"
,
i
)
}
}
The output of running the above example is:
value: 2
value: 3
value: 4
index: 0
index: 1
index: 2
Others
range iterating over other data structures:
Example
package
main
import
"fmt"
func
main
()
{
// Here we use range to compute the sum of 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 passes two variables, the index and the value. In the example above we did not need the element's ordinal number, so we omitted it with the blank identifier "_". Sometimes we really do need to know its index.
for
i
,
num
:=
range
nums
{
if
num
==
3
{
fmt
.
Println
(
"index:"
,
i
)
}
}
// range can also be used on the key-value pairs of a map.
kvs
:=
map
[
string
]
string
{
"a"
:
"apple"
,
"b"
:
"banana"
}
for
k
,
v
:=
range
kvs
{
fmt
.
Printf
(
"%s -> %s\n"
,
k
,
v
)
}
// range can also be used to enumerate a Unicode string. The first parameter is the index of the character, and the second is the character (the Unicode value) itself.
for
i
,
c
:=
range
"go"
{
fmt
.
Println
(
i
,
c
)
}
}
The above example produces the following output:
sum: 9
index: 1
a -> apple
b -> banana
0 103
1 111