Keyword

Go for keyword

Repeats a block using a condition, three-part clause or range clause. Omitting the condition permits an indefinite loop; exit must be coordinated. Example computes the sum from 1 through 4.

Formal Syntax & Structure

go syntaxgo
for [condition] { statements } | for init; condition; post { statements } | for variables := range operand { statements }

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  total := 0  for i := 1; i <= 4; i++ {    total += i  }  fmt.Println(total)}
Go Reference