From cd4f6fc6c7d22205636c5253ee9767b53664163c Mon Sep 17 00:00:00 2001 From: vinchent Date: Wed, 18 Sep 2024 09:33:38 +0200 Subject: [PATCH] concurrency: add timeout, pass the url as argument into goroutine, but still have concurrent write issue --- concurrency/concurrency.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/concurrency/concurrency.go b/concurrency/concurrency.go index a6a95ca..e9e222a 100644 --- a/concurrency/concurrency.go +++ b/concurrency/concurrency.go @@ -1,14 +1,17 @@ package concurrency +import "time" + type WebsiteChecker func(string) bool func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool { results := make(map[string]bool) for _, url := range urls { - go func() { - results[url] = wc(url) - }() + go func(u string) { + results[u] = wc(u) + }(url) } + time.Sleep(2 * time.Second) return results }