LESSON · 1 OF 20

Go's only loop keyword

Go's only loop keyword

Go has exactly one loop keyword: for. There is no while, no do-while, and no separate looping constructs. Anything you would reach for a while or a counted loop to do, you write with for.

That one keyword takes a few shapes. The same word gives you a counted loop, a condition-only loop that behaves like a while, an infinite loop, and the range form for walking over collections. The following nodes cover each shape one at a time.

Two rules hold for every shape, just like if: there are no parentheses around the loop header, and the braces are always required.

The shape to memorise: one keyword, for, several forms.

Spin up a fresh environment and practice live.
go · fresh machine · ready in under a minute