Loops
8 min · Go
Repeat work without writing it out a hundred times.
Enemy 1 of 5
for is Go's only loop keyword
Where C, C++, and C# each give you for, while, and do-while, Go simplifies this down to a single keyword: for. Written with three parts separated by semicolons — a starting statement, a condition, and a step — it behaves exactly like a classic C-style for loop: for i := 0; i < 3; i++ { ... } starts i at 0, keeps looping while i < 3 holds, and increments i by one after each pass.
That produces exactly three iterations, with i taking the values 0, 1, then 2 — the loop stops before i would reach 3, since the condition is checked before each pass runs, including the very first one.
Dropping the starting statement and step leaves you with something that behaves like a while loop from other languages (for condition { ... }), and dropping all three parts entirely (for { ... }) gives you an infinite loop you'd break out of manually — but the three-part form shown here is what you'll use for straightforward counting.
Loading editor...