A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-23.go
1packagemain
2
3import"fmt"
4
5funcmain(){
6a:=[]int{1,2,3}
7deferfunc(){fmt.Println(a)}()
8a=append(a,4)
9a[0]=99
10}
Defer
Answer & explanation
Console output
[99 2 3 4]
Why
The deferred closure captures the variable a by reference, so it observes a’s final value at function exit. By then append added 4 and a[0] = 99 changed the first element, giving [99 2 3 4]. Closures see the latest value, not the value at defer time.