Easy Tutorial
❮ Go Function Call By Value Go Function As Values ❯

Go Language For Loop

Go Language Loop Statements

The for loop is a loop control structure that can execute a specified number of iterations.

Syntax

The For loop in Go has three forms, only one of which uses semicolons.

Similar to C's for:

for init; condition; post { }

Similar to C's while:

for condition { }

Similar to C's for(;;) (infinite loop):

for { }

The execution process of the for statement is as follows:

  1. Assign an initial value to the first expression.
  2. Check if the init assignment expression meets the specified condition. If true, the loop condition is satisfied, execute the statements inside the loop, then execute post, and enter the next iteration, checking condition again. If condition is false, terminate the for loop and execute statements outside the loop.

The range format of the for loop can iterate over slices, maps, arrays, strings, etc. The format is as follows:

for key, value := range oldMap {
    newMap[key] = value
}

Both key and value in the above code can be omitted.

If you only want to read the key:

for key := range oldMap

Or:

for key, _ := range oldMap

If you only want to read the value:

for _, value := range oldMap

The syntax flow of the for statement is illustrated in the following diagram:

Example

Calculate the sum of numbers from 1 to 10:

package main

import "fmt"

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

Output:

55

The init and post parameters are optional; we can omit them, similar to a while statement.

The following example calculates the value of sum added to itself while sum is less than 10:

package main

import "fmt"

func main() {
   sum := 1
   for ; sum <= 10; {
      sum += sum
   }
   fmt.Println(sum)

   // This can also be written like this, more like a while statement
   for sum <= 10 {
      sum += sum
   }
   fmt.Println(sum)
}

Output:

16
16

Infinite loop:

package main

import "fmt"

func main() {
   sum := 0
   for {
      sum++ // Infinite loop
   }
   fmt.Println(sum) // This will not be printed
}

To stop an infinite loop, press ctrl-c in the command window.

For-each range loop

This loop format can iterate over strings, arrays, slices, etc., and output elements.

package main
import "fmt"

func main() {
   strings := []string{"google", "tutorialpro"}
   for i, s := range strings {
      fmt.Println(i, s)
   }

   numbers := [6]int{1, 2, 3, 5} 
   for i, x := range numbers {
      fmt.Printf("The value of x at position %d is = %d\n", i, x)
   }  
}

Output:

0 google
1 tutorialpro
The value of x at position 0 is = 1
The value of x at position 1 is = 2
The value of x at position 2 is = 3
The value of x at position 3 is = 5
The value of x at position 4 is = 0
The value of x at position 5 is = 0

The range format of the for loop can omit key and value, as shown in the following example:

package main
import "fmt"

func main() {
    map1 := make(map[int]float32)
    map1[1] = 1.0
    map1[2] = 2.0
    map1[3] = 3.0
    map1[4] = 4.0

    for key, value := range map1 {
        fmt.Println(key, value)
    }
}

Output:

1 1.0
2 2.0
3 3.0
4 4.0
// Read key and value
for key, value := range map1 {
    fmt.Printf("key is: %d - value is: %f\n", key, value)
}

// Read key
for key := range map1 {
    fmt.Printf("key is: %d\n", key)
}

// Read value
for _, value := range map1 {
    fmt.Printf("value is: %f\n", value)
}

The example output is:

key is: 4 - value is: 4.000000
key is: 1 - value is: 1.000000
key is: 2 - value is: 2.000000
key is: 3 - value is: 3.000000
key is: 1
key is: 2
key is: 3
key is: 4
value is: 1.000000
value is: 2.000000
value is: 3.000000
value is: 4.000000

Go Language Loop Statements

❮ Go Function Call By Value Go Function As Values ❯