A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-148.go
1packagemain
2
3import"fmt"
4
5funcmain(){
6x:=5
7deferfunc(vint){fmt.Println("captured",v)}(x)
8x=50
9fmt.Println("local",x)
10}
Defer
Answer & explanation
Console output
local 50
captured 5
Why
Even though the deferred function has a body that runs later, its argument x is evaluated when defer executes, passing 5 into parameter v. The subsequent x = 50 updates only the outer variable, which the body prints. The deferred closure prints its own frozen v = 5.