Control Flow
Go's control constructs — if/else with an init statement, the single for loop in all its forms, switch without fallthrough, range, and panic/recover.
10 questions
JuniorTheoryVery commonWhat does the program print?
What does the program print?
012
Open full question →Common mistakes
- ✗Expecting
123:range 3counts from zero, soitakes the values 0, 1 and 2. - ✗Expecting
0123or a compile error:for i := range nis a valid form offorsince Go 1.22, and it runs exactlyntimes.
Follow-up questions
- →How does a labelled
breakdiffer from a labelledcontinue? - →What scope does a variable declared in the
forinit clause have?
JuniorTheoryCommonWhat does the program print?
What does the program print?
12457
Open full question →Common mistakes
- ✗Expecting
12:continuedoes not leave the loop likebreak; it skips the rest of the current pass, so only 3 and 6 are left out. - ✗Expecting
1245: the conditioni <= 7still holds for 7, so the last pass runs and prints it.
Follow-up questions
- →What does the program print if
continueis replaced withbreak? - →How would you make the loop stop after the first number greater than 4?
JuniorTheoryCommonWhat does the program print?
What does the program print?
10
Open full question →Common mistakes
- ✗Expecting
15: the conditioni < 5is false onceireaches 5, so the body runs for 1, 2, 3 and 4 only. - ✗Expecting the running sums
1 3 6 10:fmt.Printlnsits after the loop's closing brace, so it runs once, when the loop is over.
Follow-up questions
- →How would you change the loop so that it adds 1 through 5?
- →How would you write the same loop with only a condition after
for?
JuniorTheoryCommonWhat happens when this program is built and run?
What happens when this program is built and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
11: a variable declared in theifinit statement exists only inside theifand itselse; after the closing bracevis undefined. - ✗Expecting a zero-valued
vafter theif: there is no outervat all, so the compiler stops withundefined: v.
Follow-up questions
- →How would you change the program so that it prints
11? - →Which other statements in Go let you declare a variable for that statement only?
JuniorTheoryCommonHow do if/else work in Go, including the init statement?
How do if/else work in Go, including the init statement?
Go's if takes a bare boolean condition: no parentheses, yet braces are always required. An optional init statement like if x := f(); cond {} runs first, and its name is scoped to the if/else only. else stays on the closing brace's line.
Common mistakes
- ✗Wrapping the condition in parentheses or dropping the braces, C-style
- ✗Reading an init-statement variable after the
if/elseblock ends - ✗Putting
elseon its own line below the closing brace
Follow-up questions
- →Why does Go reject
elseon a new line — what rule causes it? - →How does the
if-init scope interact with shadowing an outer name?
JuniorTheoryCommonWhat does for range iterate over and what does it yield?
What does for range iterate over and what does it yield?
for range walks built-in collections: over a slice or array it yields (index, value), over a map (key, value) in random order, over a string a byte index plus the rune, and over a channel each value until it closes. _ drops a value you skip.
Common mistakes
- ✗Expecting
(value, index)instead of Go's(index, value)order - ✗Assuming map ranging visits keys in a stable or sorted order
- ✗Thinking a string range yields bytes rather than rune code points
Follow-up questions
- →Why does a string range step by byte index but yield runes?
- →When does ranging a channel stop, and what closes that loop?
JuniorTheoryCommonWhat does the program print?
What does the program print?
a
Open full question →Common mistakes
- ✗Expecting
ab: Go runs only the first case whose condition is true and then leaves theswitch; it never falls into the next case on its own. - ✗Expecting a compile error for a missing
break: Go needs nobreakat the end of a case, because leaving theswitchis the default.
Follow-up questions
- →What does the program print if you add
fallthroughafterfmt.Print("a")? - →How would you list several values in one
caseof aswitchwith a tag?
JuniorTheoryCommonWhat does the program print?
What does the program print?
2
Open full question →Common mistakes
- ✗Expecting nothing:
fallthroughenters the next case without checking its value, socase 3runs althoughxis 2. - ✗Expecting
3or a compile error:fallthroughonly moves to the next case and leavesxas it is, and a case may hold justfallthrough.
Follow-up questions
- →What does
fallthroughdo to the next case's own condition check? - →How does a type
switchdiffer from this value-matching switch?
JuniorTheoryOccasionalWhat do panic and recover do, and where does recover work?
What do panic and recover do, and where does recover work?
panic stops normal flow and unwinds the stack, running deferred calls on the way up; unhandled, it crashes. recover works only inside a deferred function: it returns the panic value and stops the unwinding. Use both only for exceptional cases.
Common mistakes
- ✗Expecting
recoverto only hide the crash message: in a deferred function it stops the unwinding, and the function returns normally to its caller. - ✗Expecting
recoverto work anywhere in the body: outside a deferred function it returnsnil, and the panic goes on.
Follow-up questions
- →What does
recoverreturn when no panic is currently in flight? - →How does a panic in one goroutine affect the others in the program?
MiddleTheoryOccasionalWhen does an expressionless switch replace an if-else chain?
When does an expressionless switch replace an if-else chain?
An expressionless switch {} carries no value, so each case is its own boolean test. Go runs the first true case top to bottom, then exits — cleaner than a long if/else if chain. An explicit fallthrough runs the next case unconditionally.
Common mistakes
- ✗Thinking every true case runs, not just the first matched one
- ✗Believing
fallthroughre-checks the next case's condition before running - ✗Adding a value after
switchwhen an expressionless form is intended
Follow-up questions
- →How does
fallthroughbehave if it sits in the final case of a switch? - →When is a long
if/else ifchain still clearer than this switch?