Go has only one looping construct, the for loop. The basic for loop looks as it does in C or Java, except that the ( ) are gone (they are not even optional) and the { } are required. As in C or Java, you can leave the pre and post statements empty.
for init; condition; post {
}
Examples
Example - Go loop
for j := 7; j <= 9; j++ {
fmt.Println(j)
}
Example - Go loop as while loop
i := 1
for i <= 3 {
fmt.Println(i)
i = i + 1
}
Example - Go loop without pre/post statement
i := 1
for ; i < 1000; {
i *= 2
}
Example - infinite loop
for {
// do something in a loop forever
}
Example - nested loop
for i := 0; i <= 10; i++ {
for j := 0; j < 3; j++ {
fmt.Printf("Outer loop: %d\tInner loop: %d\n", i, j)
}
}