Go language for loop
A for loop is a loop control structure that can execute a loop a specified number of times.
Syntax
Go's for loop has 3 forms, only one of which uses semicolons.
Same as C's for:
for init; condition; post { }
Same as the while loop in C:
for condition { }
Same as for(;;) in C:
for { }
- init: generally an assignment expression, assigning an initial value to the control variable;
- condition: a relational expression or logical expression, the loop control condition;
- post: generally an assignment expression, incrementing or decrementing the control variable.
The execution process of the for statement is as follows:
1. First, assign an initial value to expression 1;
2. Determine whether the assignment expression init satisfies the given condition. If its value is true and the loop condition is met, execute the statements in the loop body, then execute post, enter the second loop, and judge condition again; otherwise, if the value of condition is false and the condition is not met, terminate the for loop and execute the statements outside the loop body.
The range format of the for loop can iterate over slices, maps, arrays, strings, and more. The format is as follows:
for key, value := range oldMap {
newMap[key] = value
}
In the above code, key and value can be omitted.
If you only want to read the key, the format is as follows:
for key := range oldMap
Or like this:
for key, _ := range oldMap
If you only want to read the value, the format is as follows:
for _, value := range oldMap
The syntax flow of the for statement is shown in the figure below:

Example
Calculate the sum of numbers from 1 to 10:
Example
import "fmt"
func main() {
sum := 0
for i := 0; i <= 10; i++ {
sum += i
}
fmt.Println(sum)
}
The output result is:
55
The init and post parameters are optional. We can omit them directly, similar to a While statement.
The following example calculates the value of sum after self-addition when sum is less than 10:
Example
import "fmt"
func main() {
sum := 1
for ; sum <= 10; {
sum += sum
}
fmt.Println(sum)
// This way also works, more like a while statement form
for sum <= 10{
sum += sum
}
fmt.Println(sum)
}
The output result is:
16 16
Infinite loop:
Example
import "fmt"
func main() {
sum := 0
for {
sum++ // Loop indefinitely
}
fmt.Println(sum) // Cannot output
}
To stop an infinite loop, you can press in the command windowctrl-c 。
For-each range loop
This form of loop can iterate and output elements of strings, arrays, slices, and so on.
Example
import "fmt"
func main() {
strings := []string{"google", "example"}
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 = %d\n", i,x)
}
}
The output result of the above example is:
0 google 1 example 第 0 位 x 的值 = 1 第 1 位 x 的值 = 2 第 2 位 x 的值 = 3 第 3 位 x 的值 = 5 第 4 位 x 的值 = 0 第 5 位 x 的值 = 0
The range format of the for loop can omit key and value, as shown in the following example:
Example
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
// 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 output result of the above example 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.000000other extensions