🔁 Go For Loop – Iterate Efficiently with Range, Conditions & Infinite Loops (2025 Guide)
🧲 Introduction – What Is a for Loop in Go?
Go’s for loop is the only looping construct in the language, but it’s incredibly versatile. With it, you can perform traditional counted loops, condition-based loops, infinite loops, and even iterate over collections using range.
🎯 In this section, you’ll learn:
- All valid forms of
forloops in Go - How to loop through arrays, slices, maps, and strings using
range - How to break, continue, and control flow inside loops
- Best practices and real-world examples
✅ Basic Syntax – Traditional For Loop
for init; condition; post {
// block of code
}
Example:
for i := 1; i <= 5; i++ {
fmt.Println(i)
}
📤 Output:
1
2
3
4
5
🔄 While-Style For Loop
Go does not have a while keyword, but the for loop can act like one:
i := 0
for i < 3 {
fmt.Println(i)
i++
}
📤 Output:
0
1
2
✅ This behaves exactly like a traditional while loop.
♾️ Infinite Loop
for {
fmt.Println("Running forever...")
}
❗ Be cautious—use a
breakto exit when needed.
🔁 Loop with range – Iterate Collections
Use range to iterate over arrays, slices, maps, strings, and channels.
✅ Array/Slice:
nums := []int{10, 20, 30}
for i, v := range nums {
fmt.Printf("Index %d = %d\n", i, v)
}
📤 Output:
Index 0 = 10
Index 1 = 20
Index 2 = 30
✅ Map:
m := map[string]int{"a": 1, "b": 2}
for key, val := range m {
fmt.Printf("%s → %d\n", key, val)
}
✅ String (Unicode-safe):
for i, ch := range "Go💡" {
fmt.Printf("%d: %c\n", i, ch)
}
✅ range on strings handles Unicode characters correctly.
🧼 Break, Continue, and Label
🔚 Break:
for i := 0; i < 10; i++ {
if i == 5 {
break
}
fmt.Println(i)
}
📤 Stops the loop when i == 5.
🔁 Continue:
for i := 1; i <= 5; i++ {
if i%2 == 0 {
continue
}
fmt.Println(i)
}
📤 Skips even numbers.
🏷️ Labeled Break (for nested loops):
Outer:
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if i*j > 4 {
break Outer
}
fmt.Println(i, j)
}
}
📤 Exits both loops when condition matches.
🧠 Best Practices
| Practice | Why? |
|---|---|
Prefer range for collections | More readable and idiomatic |
| Use labels sparingly | Only for breaking nested loops |
| Avoid infinite loops unless needed | They can hang processes without control |
📌 Summary – Recap & Next Steps
Go’s for loop is simple yet powerful. Whether you need a basic counter, a while-style condition, or to iterate over collections, the for construct handles it all efficiently and elegantly.
🔍 Key Takeaways:
foris the only loop in Go but supports multiple forms- Use
rangefor idiomatic iteration over slices, maps, strings, etc. - Use
break,continue, andlabelsto control loop behavior - Infinite loops require manual exit conditions
⚙️ Next: Learn about Go Functions to modularize and reuse logic efficiently.
❓ FAQs – Go For Loop
❓ Is there a while loop in Go?
✅ No. Use for with only a condition: for condition {}
❓ Can I iterate over a string with range?
✅ Yes. It handles Unicode characters properly.
❓ What does range return in a map?
✅ It returns key, value pairs for each map entry.
❓ How can I skip the current iteration?
✅ Use continue to skip to the next loop cycle.
❓ Can I break out of nested loops?
✅ Yes. Use labeled break to exit outer loops.
Share Now :
