Go Ranges
In Go programming, range keyword is used in conjunction with Go For Loop to iterate over the items of a collection.
Range can be used with an array, string, map or channels. During each iteration, range can return one or two values based on the type of collection you are iterating.
In the following table, an overview of what range returns during iteration is given.
Range Expression | 1st Value | 2nd Value |
---|---|---|
Array or slice | index | element |
String | index | rune int (int32 value representing the character value) |
Map | key | value |
Channel | element | none |
Using second value returned by the range is optional. Going forward in this article, we will learn how to use golang range with different range expressions mentioned in the above table.
Go Range with Array
In the following examples, we will take an array and iterate over the elements using for loop and range.
Examples
In the following program, we will use index only returned from range over array to iterate over the elements of array.
example.go
package main
import "fmt"
func main() {
var evens = [5] int {2, 4, 6, 8, 10}
// using index returned from range
for index:= range evens {
fmt.Printf("evens[%d] = %d \n", index, evens[index])
}
}
Output
evens[0] = 2
evens[1] = 4
evens[2] = 6
evens[3] = 8
evens[4] = 10
In the following program, we will use both index and element returned from Range over Array.
example.go
package main
import "fmt"
func main() {
var evens = [5] int {2, 4, 6, 8, 10}
// using index and element returned from range
for index,element:= range evens {
fmt.Printf("evens[%d] = %d \n", index, element)
}
}
Output
evens[0] = 2
evens[1] = 4
evens[2] = 6
evens[3] = 8
evens[4] = 10
Golang Range with String
In the following examples, we will take a String and iterate over the characters of it, using for loop and range.
Example 1 – Range & String
In this example, we will iterate over characters of a given string using range.
example.go
package main
import "fmt"
func main() {
var str = "Golang"
// using index returned from range
for index:= range str {
fmt.Printf("str[%d] = %d \n", index, str[index])
}
}
Output
str[0] = 71
str[1] = 111
str[2] = 108
str[3] = 97
str[4] = 110
str[5] = 103
The element is the rune, which is the int32 ASCII value of the character.
Example 2: Range & String
In this example, we will use range to get both the index and character during each iteration over a string.
example.go
package main
import "fmt"
func main() {
var str = "Golang"
// using index and element returned from range
for index,element:= range str {
fmt.Printf("str[%d] = %d \n", index, element)
}
}
Output
str[0] = 71
str[1] = 111
str[2] = 108
str[3] = 97
str[4] = 110
str[5] = 103
Golang Range with Map
In the following examples, we will take a Map and iterate over the key:value pairs of it, using for loop and range.
Example 1 – Range & Map
In this example, we will iterate over elements of a given Golang Map with index.
example.go
package main
import "fmt"
func main() {
var colorMap = make(map[string]string)
colorMap["white"] = "#FFFFFF"
colorMap["black"] = "#000000"
colorMap["red"] = "#FF0000"
colorMap["blue"] = "#0000FF"
colorMap["green"] = "#00FF00"
for color := range colorMap {
fmt.Println("Hex value of", color, "is", colorMap[color])
}
}
Output
Hex value of white is #FFFFFF
Hex value of black is #000000
Hex value of red is #FF0000
Hex value of blue is #0000FF
Hex value of green is #00FF00
The element is the rune, which is the int32 ASCII value of the character.
Example 2 – Key-Value from Range over Map
In this example, we will iterate over elements of a given Golang Map. During an iteration, we get both key and value using range.
example.go
package main
import "fmt"
func main() {
var colorMap = make(map[string]string)
colorMap["white"] = "#FFFFFF"
colorMap["black"] = "#000000"
colorMap["red"] = "#FF0000"
colorMap["blue"] = "#0000FF"
colorMap["green"] = "#00FF00"
for color, hex := range colorMap {
fmt.Println("Hex value of", color, "is", hex)
}
}
Output
Hex value of white is #FFFFFF
Hex value of black is #000000
Hex value of red is #FF0000
Hex value of blue is #0000FF
Hex value of green is #00FF00
Conclusion
In this Golang Tutorial, we learned about Ranges in Go language and how to use them with the help of example programs.