Loops in Go

From the Go cheat sheet Β· Control Flow Β· verified Jul 2026

Loops

For loops and iterations

go
// Basic for loop
for i := 0; i < 10; i++ {
    fmt.Println(i)
}

// While-style loop
for x < 100 {
    x *= 2
}

// Range over slice
for i, v := range slice {
    fmt.Printf("%d: %v\n", i, v)
}
πŸ”„ Only for loops (no while/do-while)
πŸ“Š range for iterating collections
🏷️ Labels for breaking nested loops
⚑ range on channels until closed

More Go tasks

Back to the full Go cheat sheet