concurrency: add timeout, pass the url as argument into goroutine, but still have concurrent write issue

This commit is contained in:
vinchent 2024-09-18 09:33:38 +02:00
parent cb8e6d23f3
commit cd4f6fc6c7

View File

@ -1,14 +1,17 @@
package concurrency package concurrency
import "time"
type WebsiteChecker func(string) bool type WebsiteChecker func(string) bool
func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool { func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
results := make(map[string]bool) results := make(map[string]bool)
for _, url := range urls { for _, url := range urls {
go func() { go func(u string) {
results[url] = wc(url) results[u] = wc(u)
}() }(url)
} }
time.Sleep(2 * time.Second)
return results return results
} }