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 {
...
}
for {
...
}
There is no
while
keyword in Go, so all “while” loops can be created with thefor
keyword.
Example:
package main
import (
"fmt"
"time"
)
func main() {
for {
fmt.Println("https://gosamples.dev is the best")
time.Sleep(1 * time.Second)
}
}