I'm a golang newbie and currently working on Exercise: Web Crawler.
I simply put the keyword 'go' before every place where func Crawl is invoked and hope it can be parallelized, but fmt.Printf
doesn't work and prints nothing. Nothing other is changed on the original code besides this one. Would someone like to give me a hand?
func Crawl(url string, depth int, fetcher Fetcher) {
// TODO: Fetch URLs in parallel.
// TODO: Don't fetch the same URL twice.
// This implementation doesn't do either:
if depth <= 0 {
return
}
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("found: %s %q
", url, body)
for _, u := range urls {
go Crawl(u, depth-1, fetcher)
}
return
}
func main() {
go Crawl("https://golang.org/", 4, fetcher)
}