Written by
Edd Turtle
on gophercoding.com
on
on
(Updated: )
While True Loop in Go
While loops do not actually exist within Go (golang), by name, but the same functionality is achievable with a for
loop. Which is great, why have a ‘while’ when the same can be done with ‘for’.
Our example below shows how you get a Go program to print out a message every second to the screen. (You’ll have to exit to application to stop it, e.g. Ctrl+C)
|
|
If you need to exit this loop, if something went wrong, or a condition was meet then use the break
clause.
|
|
Equally, if we need to check on a condition, we can replace the true with a variable.
|
|