This question already has an answer here:
- Golang defer behavior 3 answers
golang's documentation says this:
A deferred function's arguments are evaluated when the defer statement is evaluated.
This confuses me.
Question: Does "evaluated" mean that the value is already known? I just don't understand why two examples print different. This confuse me.
I have two examples below:
//It prints 0.
func deferA() {
i := 0
defer fmt.Println(i)
i++
return
}
//It prints 1.
func deferB() {
i := 0
defer func() {
fmt.Println(i)
}()
i++
return
}
</div>