-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Open
Description
I'm learning go here, so might be way off the mark on this :)
the code in this block
package concurrency
type WebsiteChecker func(string) bool
type result struct {
string
bool
}
func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
results := make(map[string]bool)
resultChannel := make(chan result)
for _, url := range urls {
go func() {
resultChannel <- result{url, wc(url)}
}()
}
for i := 0; i < len(urls); i++ {
r := <-resultChannel
results[r.string] = r.bool
}
return results
}
doesn't pass the initial (correctness) test - all the values within the channel have the last url and url result within them. I actually get a warning in goland about the code

I don't fully understand that message but I think the value of the url
is changing before the goroutine has had a chance to execute. There is more info here.
The below code passes all tests
for _, url := range urls {
finalUrl := url
go func() {
resultChannel <- result{finalUrl, wc(finalUrl)}
}()
}
I haven't raised a PR as my code might not be the "correct" answer, but welcome any feedback
Metadata
Metadata
Assignees
Labels
No labels