Skip to content

Loop Control

In Go, there is only one loop statement: for. Go abandons the while statement, and the for statement can be used as while.

for

The statement format is as follows:

go
for init statement; expression; post statement {
  execute statement
}

When only the loop condition is retained, it becomes while.

go
for expression {
  execute statement
}

This is an infinite loop that will never exit:

go
for {
  execute statement
}

Example

This is code that outputs numbers in the range [0,20]:

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

You can initialize multiple variables at the same time and increment them:

go
for i, j := 1, 2; i < 100 && j < 1000; i, j = i+1, j+1 {
  fmt.Println(i, j)
}

Used as while:

go
num := 1
for num < 100 {
    num *= 2
}

Nested loops to print the multiplication table (a classic loop example):

go
func main() {
  for i := 1; i <= 9; i++ {
    for j := 1; j <= 9; j++ {
      if i <= j {
        fmt.Printf("%d*%d = %2d  ", i, j, i*j)
      }
    }
    fmt.Println()
  }
}

Output:

go
1*1 =  1  1*2 =  2  1*3 =  3  1*4 =  4  1*5 =  5  1*6 =  6  1*7 =  7  1*8 =  8  1*9 =  9
2*2 =  4  2*3 =  6  2*4 =  8  2*5 = 10  2*6 = 12  2*7 = 14  2*8 = 16  2*9 = 18
3*3 =  9  3*4 = 12  3*5 = 15  3*6 = 18  3*7 = 21  3*8 = 24  3*9 = 27
4*4 = 16  4*5 = 20  4*6 = 24  4*7 = 28  4*8 = 32  4*9 = 36
5*5 = 25  5*6 = 30  5*7 = 35  5*8 = 40  5*9 = 45
6*6 = 36  6*7 = 42  6*8 = 48  6*9 = 54
7*7 = 49  7*8 = 56  7*9 = 63
8*8 = 64  8*9 = 72
9*9 = 81

for range

for range can more conveniently iterate over some iterable data structures, such as arrays, slices, strings, maps, and channels. The statement format is as follows:

go
for index, value := range iterable {
  // body
}

index is the index of the iterable data structure, and value is the value at the corresponding index. For example, using for range to iterate over a string:

go
func main() {
   sequence := "hello world"
   for index, value := range sequence {
      fmt.Println(index, value)
   }
}

for range can also iterate over an integer value. Literals, constants, and variables are all valid.

go
for i := range 10 {
    fmt.Println(i)
}

n := 10
for i := range n {
    fmt.Println(i)
}

const n = 10
for i := range n {
  fmt.Println(i)
}

For each data structure, the implementation of for range is different. This will be covered later. You can visit Go - for statement for more details.

break

The break keyword terminates the innermost for loop. When used together with a label, it can terminate the outer loop. Here is an example: This is a nested loop:

go
func main() {
  for i := 0; i < 10; i++ {
    for j := 0; j < 10; j++ {
      if i <= j {
        break
      }
      fmt.Println(i, j)
    }
  }
}

Output:

1 0
2 0
2 1
3 0
3 1
3 2
...
9 6
9 7
9 8

Using a label to break the outer loop:

go
func main() {
Outer:
  for i := 0; i < 10; i++ {
    for j := 0; j < 10; j++ {
      if i <= j {
        break Outer
      }
      fmt.Println(i, j)
    }
  }
}

Output:

continue

The continue keyword skips the current iteration of the innermost loop and directly proceeds to the next iteration. When used together with a label, it can skip the iteration of the outer loop. Here is an example:

go
func main() {
  for i := 0; i < 10; i++ {
    for j := 0; j < 10; j++ {
      if i > j {
        continue
      }
      fmt.Println(i, j)
    }
  }
}

Output:

0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
...
7 7
7 8
7 9
8 8
8 9
9 9

Using a label:

go
func main() {
Out:
  for i := 0; i < 10; i++ {
    for j := 0; j < 10; j++ {
      if i > j {
        continue Out
      }
            fmt.Println(i, j)
    }
  }
}

Output:

0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9

Golang by www.golangdev.cn edit