Skip to content

Instantly share code, notes, and snippets.

@JPMoresmau
Created January 29, 2019 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPMoresmau/9ddb752da350ebeab3e49fc8cdc57ce5 to your computer and use it in GitHub Desktop.
Save JPMoresmau/9ddb752da350ebeab3e49fc8cdc57ce5 to your computer and use it in GitHub Desktop.
How closure don't capture loop value
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