For loop examples in Golang

  • 13 April 2020
  • ADM

 

For loop examples in Golang - images/logos/golang.jpg

 

Similar with other programing languages, the basic for loop has three components separated by semicolons:

  • the init statement: executed before the first iteration
  • the condition expression: evaluated before every iteration
  • the post statement: executed at the end of every iteration

Table of content

 


Syntax

The basic syntax is:

for init; condition; post { 
  // code block that it will run as long the condition is true
}

Infinite loop syntax:

for {
   // code block that it will run forever 
}

Note: Unlike other programing languages there are no parentheses surrounding the three components of the for statement. The braces { } are mandatory.

 

For loop example

package main

import "fmt"

func main() {
	fmt.Println("For loop example")
	fmt.Println()

	for i := 0; i < 10; i++ {
		fmt.Println("For loop iteration ", i, ".")
	}

}

Output

For loop example

For loop iteration  0 .
For loop iteration  1 .
For loop iteration  2 .
For loop iteration  3 .
For loop iteration  4 .
For loop iteration  5 .
For loop iteration  6 .
For loop iteration  7 .
For loop iteration  8 .
For loop iteration  9 .

For range loop example

package main

import "fmt"

func main() {
	fmt.Println("For range loop example")
	fmt.Println()

	days := [...]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}

	for i, val := range days {
		fmt.Println("For loop iteration ", i, ", value: ", val)
	}

	fmt.Println()

	// if you don't need the index
	for _, val := range days {
		fmt.Println("For loop iteration, value: ", val)
	}
}

Output

For range loop example

For loop iteration  0 , value:  Monday
For loop iteration  1 , value:  Tuesday
For loop iteration  2 , value:  Wednesday
For loop iteration  3 , value:  Thursday
For loop iteration  4 , value:  Friday
For loop iteration  5 , value:  Saturday
For loop iteration  6 , value:  Sunday 

For loop iteration, value:  Monday
For loop iteration, value:  Tuesday
For loop iteration, value:  Wednesday
For loop iteration, value:  Thursday
For loop iteration, value:  Friday
For loop iteration, value:  Saturday
For loop iteration, value:  Sunday

 

for infinite loop

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("For infinite loop example")
	fmt.Println()

	for {
		fmt.Println("I am running ... forever (Press Ctrl+c to stop)")

		//added 0.5 sec sleep per iteration, so it will not block the console.
		time.Sleep(500 * time.Millisecond)
	}
}

Output

For infinite loop example

I am running ... forever (Press Ctrl+c to stop)
I am running ... forever (Press Ctrl+c to stop)
I am running ... forever (Press Ctrl+c to stop)
I am running ... forever (Press Ctrl+c to stop)

 

Break a for loop

package main

import "fmt"

func main() {
	fmt.Println("Break For loop example")
	fmt.Println()

	it := 0
	for i := 0; i < 10; i++ {
		if i == 6 {
			it = i
			break
		}
		fmt.Println("For loop iteration ", i, ".")
	}
	fmt.Println()
	fmt.Println("I make it out of the for loop at iteration ", it)
}

Note: this example would also work if you need to skip a step, using the keyword continue.

Output

Break For loop example

For loop iteration  0 .
For loop iteration  1 .
For loop iteration  2 .
For loop iteration  3 .
For loop iteration  4 .
For loop iteration  5 .

I make it out of the for loop at iteration  6

 

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build example.go

Assuming that example.go is the name of your file.

Then depending on if you are on Linux or Windows the binary file is created.

To run the application, execute the command.

Linux

$ ./example

Windows

c:\Users\adm\go\tutorials> example.exe

If you want to compile and run the application in one single step, run the following command:

go run example.go

 

References