How to Iterate Over a Range of Integers in Golang

In Go (Golang), you can iterate over a range of integers using a for loop with a range clause. The range clause allows you to loop through the range of integers using the loop variable as the current integer value.

Example

package main

import (
  "fmt"
)

func main() {
  start := 1
  end := 5

  for i := start; i <= end; i++ {
    fmt.Println(i)
  }
}

Output

1
2
3
4
5

In this example, we use a for loop to iterate over a range of integers from start (1) to end (5) inclusive.

The loop variable i takes on the values within this range, and we print its value in each iteration.

That’s it.

Leave a Comment