Home >Backend Development >Golang >How Do Go Closures Handle For Loop Variables and Prevent Unexpected Behavior?
In Go, for loops are not like foreach loops in C#, where loop variables cannot change within the loop body. In Go, loop variables can be modified, prompting the need to handle captured closure behavior differently.
To illustrate, let's examine three code snippets:
Lab1:
l := [](func() (int32, int32)){} for k, v := range m { // Closure captures the last values assigned to k and v from the range l = append(l, func() (int32, int32) { return k, v }) }
Lab2:
l := [](func() (int32, int32)){} for k, v := range m { // Closure captures the outer variables k and v (which are modified in the loop) l = append(l, func() (int32, int32) { return k, v }) }
Lab3:
l := [](func() (int32, int32)){} for k, v := range m { kLocal, vLocal := k, v // Captures just the values assigned to k and v at the iteration l = append(l, func() (int32, int32) { return kLocal, vLocal }) }
In Lab1 and Lab2, the closures capture the last values assigned to loop variables within the loop body. This can lead to incorrect results if loop variables are modified within the loop.
Lab3 demonstrates the correct approach. It captures the values assigned to loop variables specifically within each iteration using local variables. This behavior is consistent with closures in Go, where captured variables are by default referentially transparent.
In Go, closures over for loop variables capture values rather than references. To capture values that may change during the loop, it is necessary to create local variables within each iteration that assign the desired values to the closure. This approach ensures the closure behavior meets expectations.
The above is the detailed content of How Do Go Closures Handle For Loop Variables and Prevent Unexpected Behavior?. For more information, please follow other related articles on the PHP Chinese website!