Go for range Loop

Summary: in this tutorial, you will learn how to use the Go for range loop to iterate over elements in a collection including an array and slice.

Basic for range loop

Go supports the for loop statement that allows you to iterate over elements of a collection such as an array or a slice.

Additionally, Go offers the for range loop statement that provides a cleaner and more readable way to access both the index and value of each element.

Here’s the syntax of the Go for range loop:

for index, value := range collection {
   // loop body
}Code language: Go (go)

In this syntax:

  • index: represents the index of the current element in the collection.
  • value: represents the value of the current element in the collection.
  • range: is a keyword that instructs Go that we are about to iterate over elements of a collection.
  • collection: is the collection variable we want to iterate e.g., an array or slice.

The following example shows how to use the for range loop to iterate over elements of an array:

package main

import "fmt"

func main() {
    scores := [...] int {1, 2, 3, 4, 5}
    
    for index, value := range scores {
        fmt.Printf("Index: %d, Value: %d\n", index, value)
    }
}Code language: Go (go)

Output:

Index: 0, Value: 1
Index: 1, Value: 2
Index: 2, Value: 3
Index: 3, Value: 4
Index: 4, Value: 5Code language: Go (go)

When we assign the element to the value variable within the loop, Go copies the element to the value variable. If you change the value variable inside the loop, the change will not be reflected in the elements.

For example:

package main

import "fmt"

func main() {
    scores := [...] int {1, 2, 3, 4, 5}
    
    for index, value := range scores {
        value = value * 2
        fmt.Printf("Index: %d, Value: %d\n", index, value)
    }

    fmt.Println(scores)
}Code language: Go (go)

Output:

Index: 0, Value: 2
Index: 1, Value: 4
Index: 2, Value: 6
Index: 3, Value: 8
Index: 4, Value: 10
[1 2 3 4 5]Code language: plaintext (plaintext)

In this example, we change the value variable inside the loop. However, the elements do not change in the scores array. If you want to change the array elements, you need to use a for loop:

package main

import "fmt"

func main() {
    scores := [...]int{1, 2, 3, 4, 5}
    
    for i := 0; i < len(scores); i++ {
        scores[i] *= 2
    }

    fmt.Println(scores)
}Code language: Go (go)

Output:

[2 4 6 8 10]Code language: JSON / JSON with Comments (json)

Ignoring value

If you need only the index, you can ignore the value variable in the for range loop like this:

for index := range collection {
   // loop body
}Code language: Go (go)

For example:

package main

import "fmt"

func main() {
    scores := [...]int{1, 2, 3, 4, 5}
    
    for index := range scores {
        fmt.Printf("Index: %d\n", index)
    }
}Code language: Go (go)

Output:

Index: 0
Index: 1
Index: 2
Index: 3
Index: 4Code language: Go (go)

Notice that you can use this for loop to change values in the array. For example:

package main

import "fmt"

func main() {
    scores := [...]int{1, 2, 3, 4, 5}

    for index := range scores {
        scores[index] *= 2
        fmt.Printf("Index: %d, Value: %d\n", index, scores[index])
    }

    fmt.Println(scores)
}Code language: JavaScript (javascript)

Output:

Index: 0, Value: 2
Index: 1, Value: 4
Index: 2, Value: 6
Index: 3, Value: 8
Index: 4, Value: 10
[2 4 6 8 10]Code language: CSS (css)

Ignoring index

If you don’t need the index, you can use the blank identifier _.

Note that the blank identifier _ is a special identifier you can use to discard values you don’t need.

The following example shows how to use the for range loop to iterate over elements of an array and display each on the screen:

package main

import "fmt"

func main() {
    scores := [...]int{1, 2, 3, 4, 5}
    
    for _, value := range scores {
        fmt.Printf("Value: %d\n", value)
    }
}Code language: Go (go)

Output:

Value: 1
Value: 2
Value: 3
Value: 4
Value: 5Code language: Go (go)

For range integer

Go 1.2.2 and later added support to loop over a range of integers:

for index := range n {
   // loop body
}Code language: JavaScript (javascript)

For example:

package main

import "fmt"

func main() {
    for index := range 5 {
        fmt.Println(index)
    }
}Code language: JavaScript (javascript)

Output:

0
1
2
3
4

Note that the index variable is optional. The following program display the message Hi five times without using the index variable:

package main

import "fmt"

func main() {
    for range 5 {
        fmt.Println("Hi")
    }
}Code language: JavaScript (javascript)

Output:

Hi
Hi
Hi
Hi
Hi

Summary

  • Use the for range loop to iterate over elements of a collection or a range of integers.
Was this tutorial helpful?