go-by-test/concurrency/concurrency.go

32 lines
593 B
Go

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(u string) {
resultChannel <- result{u, wc(u)}
}(url)
}
// 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
}
return results
}