Skip to content

While Loops

while.go
x := 0
for x < 10 {
fmt.Println(x)
x++
}

Consequently there is also no do while loop in Go, again you can achieve the same functionality using for loops:

do_while.go
x := 0
for {
fmt.Println(x)
x++
if x >= 10 {
break
}
}

You can create an infinite loop in Go using the for statement without any condition:

infinite_loop.go
for {
fmt.Println("This will run forever!")
}