Range Keyword in Go

In this tutorial, you will learn about how to iterate over different data types in go programming languages.

What is the range in Golang?

The range is a keyword used in the go programming language to iterate over data structures like an array, struct, channel, string, slice, maps.

How to declare a range in Golang?

The range is to loop or to iterate over some given expressions that generally evaluate an array, struct, channel, string, slice, maps. The keyword range describes the iteration.

How to use go range in array?

The below-given program shows how the range keyword is used to iterate over array data type.

Program using a range in an array


package main
import "fmt"

func main() {
        
    //array declaration
    colors := [3]string{"blue", "orange", "yellow"} 
    //range
    for idx, c := range colors {

        fmt.Println("Color -->", c, "@ index :", idx)
    }
}

Iterate over an array of string values. The array named color is declared with values “blue”,” orange”,” yellow”.The size of the array is 3. The range keyword iterates over color in each index and prints values as shown in the output.

Output:


Color --> blue @ index : 0
Color --> orange @ index : 1
Color --> yellow @ index : 2

How to use range with a slice in Golang?

Let us understand with an example. Both array and slice declaration are almost the same except that we will not mention the size of the slice during declaration.

For better understanding, you can refer to our previous for array and slice.

Program using a range in a slice


package main
import "fmt"

func main() {
    
    // Creating a slice using the var keyword
    var slice1 = []string{"learn", "e", "tutorials"}

    //range
    for idx, s := range slice1{
fmt.Println("slice1 -->", s, "@ index :", idx)
}
}

Output:


slice1 --> learn @ index : 0
slice1 --> e @ index : 1
slice1 --> tutorials @ index : 2

How to use range with map in Golang?

The below example uses range to iterate over a Golang map. In the given example range keyword iterates over a range of site maps, where the keys and the values are strings.


package main
import "fmt"

func main() {
    
   site := map[string]string{            //map declaration

        "L": "Learn", 
        "e": "e",
        "T": "Tutorials",
    } 

    for k, v := range site {

        fmt.Println(k, "=>", v)
    }

    fmt.Println("----------------------")

    for k := range site {

        fmt.Println(k, "=>", site[k])
    }

Output:


L => Learn
e => e
T => Tutorials
----------------------
L => Learn
e => e
T => Tutorials

How to use range with string in Golang?

The below example uses range to iterate over a Golang string. The range keyword iterates over a range of text strings to provide the below-given output.


package main
import ("fmt")

func main() {
/* var keyword assigns variable text with value “Golang” of string type */
  var text string = "Golang"
  
  for idx, t := range text {

        fmt.Printf(" index %d %c\n", idx, t)
    }

Output:


index 0 G
 index 1 o
 index 2 l
 index 3 a
 index 4 n
 index 5 g
Type: string, value: Golang

How to use range with Golang channels?

In go programming language a channel is a pipe used by Goroutines to communicate with each other. The below-given program uses a range keyword to iterate over the channel in golang.


package main
import "fmt"

func main() {
    
    ch := make(chan string)  // channel declaration
    go func() {

        ch <- "hello"
        ch <- "Learn"
        ch <- "eTutorials"
        
        close(ch)
    }()

    for n := range ch {   // range

        fmt.Println(n)
    }
}

Output:


hello
Learn
eTutorials