Open surjit opened 1 year ago
This isn't a bug in workerpool, it's how go works.
for _, account := range accounts { // <- here you are assigning a pointer to a variable named account
fmt.Printf("loop %+v\n", account.Username) // <- you'll always get the expected answer here
wp.Submit(func() {
// here, you are capturing the outer pointer to "account"
// by the time this function is actually run, it may have a different value. That's your problem.
fmt.Printf("go rountine %+v\n", account.Username)
})
}
This is actually clearly documented right in the sample:
for _, r := range requests {
r := r // <-- 🙂
wp.Submit(func() {
fmt.Println("Handling request:", r)
})
}
Ubuntu 20.04 LTS go version go1.19.3 linux/amd64 github.com/gammazero/workerpool v1.1.3
Here actual output
I was expecting