You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1009 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 {
// anonymous function call required because go routines must call
// a function to start. An anonymous function maintains access to
// the lexical scope in which they are defined - all the variables
// that are available at the point when you declare the anonymous
//function are also available in the body of the function.
// url := url // create a new variable to avoid capturing the loop variable.
// go func() {
// results[url] = wc(url)
// }()
go func(u string) {
resultChannel <- result{u, wc(u)} // send statement
}(url) // or just pass by value to give the func it's own url
}
for i := 0; i < len(urls); i++ {
r := <-resultChannel // receive statement
results[r.string] = r.bool
}
return results
}