go-tutorial

Go Range

To iterate over the entries of a collection in Go programming, the range keyword is used in conjunction with the Go For Loop.

An array, string, map, or channels can all be utilised with range. Depending on the sort of collection you’re iterating, range can return one or two values throughout each iteration.

An overview of what range returns during iteration is presented in the table below.

Range Expression1st Value2nd Value
Array or sliceindexelement
Stringindexrune int (int32 value representing the character value)
Mapkeyvalue
Channelelementnone

It is not required to use the second value returned by the range. In the following sections of this post, we’ll learn how to use golang range with the various range expressions listed in the table above.

Go Range with Array

We’ll use for loop and range to iterate over the items of an array in the following examples.

Examples

To iterate over the members of the array in the following programme, we will use the index alone returned by range over array.

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.

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

Range in Golang using String

We’ll use for loop and range to traverse over the characters in a String in the following examples.

Range & String (Example 1)

We’ll use range to iterate over characters in a string in this example.

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.

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

Range of Golang with a Map

We’ll use for loop and range to iterate over the key:value pairs in a Map in the following examples.

Example 1 – Range & Map

We’ll iterate through the elements of a Golang Map with index in this example.

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.

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

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published. Required fields are marked *