for

The for loop has three components, and can be used just like a for loop in C or Java. Go has no while loop because the for loop serves the same purpose when used with a single condition. Refer to the following example for more clarity:

package mainimport (   "fmt")func main() {   // Basic for loop   for i := 0; i < 3; i++ {      fmt.Println("i:", i)   }   // For used as a while loop   n := 5   for n < 10 {      fmt.Println(n)      n++   }} 

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.