Skip to main content

Loops

For loops

The only looping construct in Go is the for loop. There is no while loop, but it is actually just merged into the for loop.

Loop on condition

Basically while loop except you replace the while with for

i := 1

for i <= 3 {
  fmt.Println(i)
  i = i + 1
}
Normal for loops

The same for loop that you see in C, Java, or JavaScript without the parenthesis.

for j := 0; j < 5; j ++ {
  fmt.Prinltn(j)
}

You can break on condition as well.