Cookies management by TermsFeed Cookie Consent

♾️ Infinite loop in Go

introduction loop

Please consider supporting us by disabling your ad blocker

To define an infinite loop in Go, also known as a “while true” loop in many different languages, you need to use the for statement. The traditional for loop has the form of:

for initialization; condition; post-condition {
...
}
but when you omit the initialization, condition, and post-condition statements, you get the classic “while true” infinite loop:
for {
...
}

There is no while keyword in Go, so all “while” loops can be created with the for keyword.

Example:

package main

import (
    "fmt"
    "time"
)

func main() {
    for {
        fmt.Println("https://gosamples.dev is the best")
        time.Sleep(1 * time.Second)
    }
}

Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

πŸ’€ While loop in Golang

Learn how to construct the popular programming while loop
introduction loop

➰ Foreach loop in Go

Learn how to construct the popular programming loop
introduction loop syntax

πŸ‰ Do-while loop in Go

Learn how to simulate a popular programming loop
introduction loop