Go Tutorial
Estimated reading: 3 minutes 115 views

🔁 Go Loops – for, range, nested loops, break & continue explained

🧲 Introduction – Why Loops Are Crucial in Go

Loops help you execute code repeatedly—a fundamental need in any language. Go simplifies looping with just one keyword: for. Whether you’re iterating over arrays, creating nested loops, or controlling flow with break and continue, Go’s approach is powerful and concise.

🎯 In this guide, you’ll learn:

  • How to write different for loop formats
  • How to use range to iterate over arrays, slices, maps, and strings
  • Nested loops for multi-dimensional data
  • How break, continue, and goto control loop execution

📘 Topics Covered

🔹 Loop Type / Keyword📖 Description
🔁 Go For LoopTraditional loop using initializer, condition, and increment
🔂 Go Nested For LoopLoop within a loop for multi-dimensional iteration
🔁 Go Range LoopIterate over collections like arrays, slices, maps, or strings
Go Break / Continue / GotoKeywords to control loop execution manually

🔁 Go – For Loop (Classic)

package main
import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        fmt.Println("i =", i)
    }
}

🧠 Parts of a for loop:

  • Initialization: i := 1
  • Condition: i <= 5
  • Post-statement: i++

✅ All three parts are optional:

i := 0
for i < 5 {
    fmt.Println(i)
    i++
}

🔂 Go – Nested For Loop

package main
import "fmt"

func main() {
    for i := 1; i <= 3; i++ {
        for j := 1; j <= 2; j++ {
            fmt.Printf("i=%d, j=%d\n", i, j)
        }
    }
}

🧠 Useful for:

  • Working with 2D arrays or matrix structures
  • Building combinations or permutations

🔁 Go – Range Loop

range lets you iterate over arrays, slices, maps, and strings:

nums := []int{10, 20, 30}

for index, value := range nums {
    fmt.Printf("Index: %d, Value: %d\n", index, value)
}

🧠 Skip index or value:

for _, v := range nums {
    fmt.Println(v)
}

🔁 With strings:

for i, ch := range "Go" {
    fmt.Printf("Index: %d, Char: %c\n", i, ch)
}

⛔ Go – Break, Continue, and Goto

break: Exit the loop

for i := 1; i <= 10; i++ {
    if i == 5 {
        break
    }
    fmt.Println(i)
}

🔁 continue: Skip current iteration

for i := 1; i <= 5; i++ {
    if i == 3 {
        continue
    }
    fmt.Println(i)
}

🚀 goto: Jump to labeled line (use with caution)

i := 1
for {
    if i > 3 {
        goto end
    }
    fmt.Println(i)
    i++
}

end:
fmt.Println("Exited loop using goto")

⚠️ Best avoided unless absolutely necessary (e.g., breaking deeply nested logic).


📌 Summary – Recap & Next Steps

Go offers a flexible and simplified looping system using just for. From traditional loops to advanced iterations over collections, Go enables efficient iteration with minimal syntax.

🔍 Key Takeaways:

  • for is the only loop keyword in Go
  • Use range for easy collection iteration
  • Combine loops with conditions and logic for complex behavior
  • Use break to exit, continue to skip, and goto for explicit jumps

⚙️ Real-World Use Cases:

  • Processing JSON arrays
  • Reading files line by line
  • Looping through API responses or user input

❓ Frequently Asked Questions

Is while supported in Go?
✅ ❌ No. Use for condition instead:

for x < 10 {
    // same as while
}

How do I loop infinitely in Go?
✅ Use:

for {
    // infinite loop
}

What’s the benefit of range in Go?
✅ It allows concise and safe iteration over arrays, slices, maps, strings, and channels.


Can I label loops in Go?
✅ Yes. Labels are used with break or goto:

Outer:
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        if j == 1 {
            break Outer
        }
    }
}

Can I use continue with range?
✅ Yes. It works the same way:

for _, v := range values {
    if v == 0 {
        continue
    }
    fmt.Println(v)
}

Share Now :
Share

Go – Loops Overview

Or Copy Link

CONTENTS
Scroll to Top