Comment by a-poor
This means you can't pass variables in as function arguments. Even the example in the official go docs doesn't handle the scope correctly:
func main() {
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.example.com/",
}
for _, url := range urls {
// Launch a goroutine to fetch the URL.
wg.Go(func() {
// Fetch the URL.
http.Get(url)
})
}
// Wait for all HTTP fetches to complete.
wg.Wait()
}
https://pkg.go.dev/sync#example-WaitGroupYou need to use this pattern instead:
for _, url := range urls {
url := url
// ...
This isn’t necessary anymore as of Go 1.22
https://go.dev/blog/loopvar-preview