2024-09-18 07:16:56 +00:00
|
|
|
package concurrency
|
|
|
|
|
|
|
|
type WebsiteChecker func(string) bool
|
|
|
|
|
2024-09-18 07:48:00 +00:00
|
|
|
type result struct {
|
|
|
|
string
|
|
|
|
bool
|
|
|
|
}
|
|
|
|
|
2024-09-18 07:16:56 +00:00
|
|
|
func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
|
|
|
|
results := make(map[string]bool)
|
2024-09-18 07:48:00 +00:00
|
|
|
resultChannel := make(chan result)
|
2024-09-18 07:16:56 +00:00
|
|
|
|
|
|
|
for _, url := range urls {
|
2024-09-18 07:33:38 +00:00
|
|
|
go func(u string) {
|
2024-09-18 07:48:00 +00:00
|
|
|
resultChannel <- result{u, wc(u)}
|
2024-09-18 07:33:38 +00:00
|
|
|
}(url)
|
2024-09-18 07:16:56 +00:00
|
|
|
}
|
2024-09-18 07:48:00 +00:00
|
|
|
// XXX: it seems that this works on Go 1.23
|
|
|
|
// for _, u := range urls {
|
|
|
|
// go func() {
|
|
|
|
// resultChannel <- result{u, wc(u)}
|
|
|
|
// }()
|
|
|
|
// }
|
|
|
|
for i := 0; i < len(urls); i++ {
|
|
|
|
r := <-resultChannel
|
|
|
|
results[r.string] = r.bool
|
|
|
|
}
|
|
|
|
|
2024-09-18 07:16:56 +00:00
|
|
|
return results
|
|
|
|
}
|