Cookies management by TermsFeed Cookie Consent
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured. It's a genocide. We need your help. Let's fight back against the Russian regime.
Help Ukraine! Fight the Russian regime!

♾️ 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)
    }
}

💤 While loop in Golang

Learn how to construct the popular programming while loop
introduction loop

➰ Foreach or for range 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