Control flow

If you already know the basic control-flow tools from another language, the main thing to learn here is how Go narrows the set down and what it leaves out.

Go keeps the toolbox deliberately small: if for conditions, for for loops, and switch for multi-way branching. There is no while, no do-while, and no ternary operator. Instead, the language leans on a few simple forms that show up everywhere.

This chapter starts with if, then moves through for, range, switch, labelled break, and finally defer, Go's unusual way of scheduling cleanup code to run later. Move through the lessons in order. Later chapters assume these forms are second nature.