typeover
curriculum

Curriculum Idioms & ecosystem Defer exercise 3 · mcq

Defer

When are the arguments to a deferred call evaluated? This is the most common defer gotcha. ``go i := 1 defer fmt.Println("i =", i) i = 2 // function returns — what prints? `` Pick the answer.

TypeScript reference
Pick the idiomatic Go translation

About this theme

defer fn() schedules fn to run when the surrounding function returns. The closest TS analogue is a try/finally you don't have to indent. Multiple defers run LIFO. Arguments are evaluated at the defer site, not at the call site — a common surprise. Also: defer in a loop accumulates.