Deferred function arguments are evaluated immediately, not when the function runs:
x := 10
defer fmt.Println(x) // captures 10 now
x = 20
// prints 10, not 20
The value of x is captured when defer is called. Later changes don't affect it. This can surprise you if you expect the deferred call to see the final value. Keep this timing in mind when using defer.