Golang For Loop Examples

Golang is an extremely minimal language featuring just 25 keywords in its syntax. When working with for and while loops you will see how easy is to write them.

While loop in Go

One of the first things you may wonder when first learning about Go syntax is how you write a while loop? In programming languages like python, Java or C you can use the while keyword, but in Go we don’t have that, instead we can use the following notation.

i := 0
for i < 10 {
  fmt.Println(i)
  i++
}

As you will see later in this article, the for keyword in Go can be used in different ways, this is how you write a while loop, just by specifying a boolean condition and then a block of code which will be repeated until the boolean condition is no longer true

This will yield the following result

0
1
2
3
4
5
6
7
8
9

For loop in Go

One of the first for loops I have written when I first started to program was in C and it had 3 components. The first component was the initialisation, then there was the boolean condition which, when true would execute the next loop itearation, and finally the final component which will be evaluated after each iteration. You may already be familiar with this notation

for i := 0; i < 10; i++ {
  fmt.Println(i)
}

This will yield the same result as the example we have seen above. Let’s break it down

Range loop in Go

Go has a keyword that you may not have used before in other programming languages: the range keyword. You can use it to iterate over slices, arrays, strings and maps along with the for keyword. Let’s see an example

for index, n := range []int{6, 8, 1, 7, 9, 0, 2} {
  fmt.Println("index: ", index, " n: ", n)
}

The range keyword can be used in a for loop to iterate over the elements and indexes of an array, so the example above will yield the following result

index:  0  n:  6
index:  1  n:  8
index:  2  n:  1
index:  3  n:  7
index:  4  n:  9
index:  5  n:  0
index:  6  n:  2

For loop keywords

You might finally want to know some of the keywords you can use when working with a for loop in Go. Those can be used to skip part of the for loop or exit the for loop altogether. You can do that by using the continue and break keywords, which you may have used in other programming languages before. Let’s see an example

for i := 0; i < 20; i++ {
  if i == 7 {
      continue
  }
  if i == 12 {
    break
  }
  fmt.Println(i)
}

This will effectively skip the number 7 and stop at the number 12

0
1
2
3
4
5
6
8
9
10
11

Infinite loop in Go

The for loop keyword can also be used without any condition and used as a infinite while or for loop as you may have in other programming languages. Let’s see the example below

for {
    fmt.Println("infinite for loop in golang!")
}

This will print "infinite for loop in golang!" indefinitely on your screen

infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
infinite for loop in golang!
...
Join the Golang Developers Community on Golang Cafe