This is a creation in Article, where the information may have evolved or changed.
6. Toad Notes Go language--for loop
For
Go has only one loop structure-the ' for ' Loop.
The basic for loop does not have to be "()" (or even forced to use them), it looks like it does in C or Java, and ' {} ' is required.
Package Main
Import "FMT"
Func Main () {
Sum: =0
For i:= 0; I < 10; i++ {
sum+= I
}
Fmt. PRINTLN (SUM)
}
Perform:
45
As in C or Java, you can leave the front and back statements empty.
Package Main
Import "FMT"
Func Main () {
Sum: =1
For sum< 1000 {
sum+= sum
}
Fmt. PRINTLN (SUM)
}
Based on this, you can omit the semicolon: C while in Go is called ' for '.
Package Main
Import "FMT"
Func Main () {
Sum: =1
For sum< 1000 {
sum+= sum
}
Fmt. PRINTLN (SUM)
}
Dead loop
If the loop condition is omitted, the loop does not end, so it is possible to express the dead loop in a more concise form.
Package Main
Func Main () {
for {
}
}