×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Go Pointers

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How do I break out of an infinite loop in Golang

Last Updated : July 19, 2025

In Go, infinite loops are commonly created using the for statement without any condition. These loops run endlessly until they are interrupted by a control statement such as break, return, or os.Exit(). The break statement is the most commonly used way to exit an infinite loop.

Creating an Infinite Loop

The syntax for an infinite loop in Go is:

for {
    // loop body
}

Breaking Out of an Infinite Loop

To exit the infinite loop, use the break statement inside a conditional block. When the condition is met, the loop will terminate immediately.

Example

This example demonstrates how to break out of an infinite loop when a counter reaches a certain value:

package main
import "fmt"

func main() {
    count := 0

    for {
        fmt.Println("Count is", count)
        count++

        if count >= 5 {
            break
        }
    }

    fmt.Println("Exited the infinite loop")
}

When you run the above code, the output will be:

Count is 0
Count is 1
Count is 2
Count is 3
Count is 4
Exited the infinite loop

Exit the Loop and Function Using return

The return statement can also be used to exit the loop and the function at the same time.

Example

The following example demonstrates how to use an infinite loop in Go and exit it using the return statement:

package main
import "fmt"

func main() {
    num := 1

    for {
        if num > 3 {
            fmt.Println("Terminating function using return")
            return
        }
        fmt.Println("Number:", num)
        num++
    }
}

When you run the above code, the output will be:

Number: 1
Number: 2
Number: 3
Terminating function using return

Terminate the Program Using os.Exit()

In some cases, you may want to forcefully stop the program execution entirely. This can be done using the os.Exit() function.

Example

The following example shows how to forcefully terminate a Go program using os.Exit(), which immediately stops execution without reaching subsequent lines:

package main
import (
    "fmt"
    "os"
)

func main() {
    for {
        fmt.Println("Running...")

        // Force exit the program after one iteration
        os.Exit(0)
    }

    // This line will never be reached
    fmt.Println("This will not be printed")
}

When you run the above code, the output will be:

Running...

Exercise

Select the correct answers to test your understanding of breaking out of an infinite loop in Go.

  1. What is the primary keyword used to exit a loop in Go?
  2. What happens when you use return inside an infinite loop?
  3. Which function is used to terminate the program forcefully?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.