Created
January 29, 2019 21:05
-
-
Save JPMoresmau/9ddb752da350ebeab3e49fc8cdc57ce5 to your computer and use it in GitHub Desktop.
How closure don't capture loop value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
nums := []int{2, 3, 4} | |
funcs := make([]func(), 0) | |
for _, i := range nums { | |
funcs = append(funcs, func() { fmt.Printf("%d\n", i) }) | |
} | |
for _, f := range funcs { | |
f() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment