Skip to main content
  1. Tutorials/

♾️ Infinite loop in Go

·1 min

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