Deferring Closure Capture in Go
Go's defer statement can be used to execute a function after the surrounding function returns. However, when used with closures, it's important to understand how parameter capture works.
The Issue
Consider the following code:
package main import "fmt" func main() { var whatever [5]struct{} for i := range whatever { fmt.Println(i) } // part 1 for i := range whatever { defer func() { fmt.Println(i) }() } // part 2 for i := range whatever { defer func(n int) { fmt.Println(n) }(i) } // part 3 }
The output of the code is:
0 1 2 3 4 4 3 2 1 0 4 4 4 4 4
Analysis
Key Differences
The crucial difference between parts 2 and 3 lies in whether or not the closure captures outer variables. In part 2, the closure captures i, which is a reference to an outer variable. In part 3, the closure doesn't have any outer references, so each call has a different value of n.
Additional Considerations
The above is the detailed content of Why Does Deferring Closure Capture in Go Lead to Unexpected Behavior?. For more information, please follow other related articles on the PHP Chinese website!