Introduction
Go simplifies looping by providing only a single keyword, for, which covers every looping pattern found in other languages, including while and do-while style iteration. This design choice reduces the mental overhead of choosing between multiple loop constructs and keeps the language's syntax minimal, while still supporting classic C-style loops, condition-only loops, infinite loops, and range-based iteration over collections.
Cricket analogy: A coach who drills one basic batting stance for every situation - defense, attack, rotating strike - instead of teaching five different stances mirrors Go's single for keyword covering every loop pattern.
Syntax
// classic three-part loop
for i := 0; i < 10; i++ {
// body
}
// while-style loop
for condition {
// body
}
// infinite loop
for {
// body, use break to exit
}
// range-based loop
for index, value := range collection {
// body
}Explanation
The classic three-part for loop has an init statement, a condition, and a post statement, all optional and separated by semicolons. Omitting the init and post parts while keeping only the condition produces while-style behavior. Omitting everything, including the semicolons, produces an infinite loop that must be exited with break or a return. The range keyword iterates over arrays, slices, strings, maps, and channels, yielding an index or key plus the corresponding value; for maps the iteration order is randomized by design, and for channels range yields values until the channel is closed.
Cricket analogy: Setting a bowler's field with an initial placement, a condition to bowl until 6 overs are done, and a post-over adjustment mirrors the three-part for loop; dropping init and post for just 'while wickets remain' gives while-style overs, and range over the batting lineup mirrors listing each batsman with their score.
Example
package main
import "fmt"
func main() {
// classic loop
sum := 0
for i := 1; i <= 5; i++ {
sum += i
}
fmt.Println("sum:", sum)
// while-style loop
n := 8
for n > 1 {
if n%2 == 0 {
n /= 2
} else {
n = 3*n + 1
}
}
fmt.Println("final n:", n)
// range over a slice
fruits := []string{"apple", "banana", "cherry"}
for i, f := range fruits {
fmt.Println(i, f)
}
}Output
sum: 15
final n: 1
0 apple
1 banana
2 cherryKey Takeaways
- Go has only one looping keyword: for.
- Omitting init and post creates a while-style loop.
- Omitting everything creates an infinite loop, exited with break.
- range yields index/key plus value for slices, arrays, strings, maps, and channels.
- Map iteration order via range is intentionally randomized each run.
Practice what you learned
1. How many looping keywords does Go provide?
2. What does 'for condition { }' behave like in Go?
3. How do you exit an infinite 'for { }' loop in Go?
4. What two values does 'for i, v := range slice' provide on each iteration?
5. What is notable about the iteration order when using range on a map?
Was this page helpful?
You May Also Like
Labeled Loops and goto in Go
Learn how labeled break/continue control nested loops in Go, and the limited, rarely used goto statement.
Slices in Go
A dynamically-sized, flexible view into an underlying array, the go-to collection type in idiomatic Go.
Maps in Go
An unordered collection of key-value pairs providing fast lookup, insertion, and deletion by key.
if-else Statements in Go
Learn how Go's if-else statement works without parentheses, including its unique init-statement syntax.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics