Golang for keyword
last modified May 7, 2025
This tutorial explains how to use the for
keyword in Go. We'll
cover loop basics with practical examples of iteration in Go programming.
The for statement is Go's only looping construct. It can be used in three forms: as a traditional for loop, while loop, and infinite loop.
In Go, for
provides all looping functionality you need. It's
versatile and can iterate over collections, implement condition-based loops,
and create infinite loops.
Basic for loop
The traditional for loop has initialization, condition, and post statements. This example demonstrates counting from 0 to 4.
package main import "fmt" func main() { for i := 0; i < 5; i++ { fmt.Println(i) } }
The loop initializes i
to 0, runs while i < 5
, and
increments i
after each iteration. This is the most common form.
While-style for loop
Go's while loop is just a for loop with only a condition. This example shows how to implement while-loop behavior.
package main import "fmt" func main() { count := 0 for count < 5 { fmt.Println(count) count++ } }
The loop continues as long as count < 5
. The counter must be
managed inside the loop body, similar to traditional while loops.
Infinite for loop
An infinite loop omits all three statements. This example shows how to create and break from an infinite loop.
package main import "fmt" func main() { i := 0 for { fmt.Println(i) i++ if i > 4 { break } } }
The loop runs indefinitely until the break condition is met. This pattern is useful for servers or long-running processes.
For-range loop
The range form iterates over slices, arrays, maps, strings, or channels. This example demonstrates iterating over a slice.
package main import "fmt" func main() { fruits := []string{"apple", "banana", "cherry"} for index, fruit := range fruits { fmt.Printf("%d: %s\n", index, fruit) } }
The range returns both index and value for each element. You can omit the
index using _
if only the value is needed.
Nested for loops
For loops can be nested to handle multi-dimensional data structures. This example shows a multiplication table.
package main import "fmt" func main() { for i := 1; i <= 3; i++ { for j := 1; j <= 3; j++ { fmt.Printf("%d * %d = %d\t", i, j, i*j) } fmt.Println() } }
The outer loop controls rows while the inner loop handles columns. Each iteration of the outer loop triggers a complete inner loop cycle.
For loop with continue
The continue statement skips to the next iteration. This example skips even numbers in a loop.
package main import "fmt" func main() { for i := 0; i < 10; i++ { if i%2 == 0 { continue } fmt.Println(i) } }
When i
is even, continue skips the print statement. Only odd
numbers between 0 and 9 are printed.
Labeled for loops
Labels allow breaking or continuing outer loops from nested structures. This example demonstrates breaking from an outer loop.
package main import "fmt" func main() { OuterLoop: for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Printf("i=%d, j=%d\n", i, j) if i == 1 && j == 1 { break OuterLoop } } } }
The label OuterLoop:
marks the outer loop. The break statement
exits both loops when the condition is met.
Source
This tutorial covered the for
keyword in Go with practical
examples of different looping scenarios and patterns.
Author
List all Golang tutorials.