A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-71.go
1packagemain
2
3import"fmt"
4
5funcmain(){
6results:=[]int{}
7fori:=range3{
8func(){
9deferfunc(){results=append(results,i)}()
10}()
11}
12fmt.Println(results)
13}
Iteration
Answer & explanation
Console output
[0 1 2]
Why
Each iteration invokes an anonymous function whose deferred closure appends i; the defer runs as that inner function returns, once per iteration. Under Go 1.22 per-iteration scoping, each captured i is distinct (0, 1, 2), so results becomes [0 1 2]. The order matches iteration order because each defer fires within its own iteration.