I'm going through 'A Tour of Go' and have been editing most of the lessons to make sure I fully understand them. I have a question regarding an answer provided to the following exercise: https://tour.golang.org/concurrency/10 which can be found here: https://github.com/golang/tour/blob/master/solutions/webcrawler.go
I have a question regarding the following section:
done := make(chan bool)
for i, u := range urls {
fmt.Printf("-> Crawling child %v/%v of %v : %v.
", i, len(urls), url, u)
go func(url string) {
Crawl(url, depth-1, fetcher)
done <- true
}(u)
}
for i, u := range urls {
fmt.Printf("<- [%v] %v/%v Waiting for child %v.
", url, i, len(urls), u)
<-done
}
fmt.Printf("<- Done with %v
", url)
What purpose does adding and removing true from the the channel done
and running the two separate for loops have? Is it just to block until the go routine finishes? I know this is an example exercise, but doesn't that kind of defeat the point of spinning out a new thread in the first place?
Why can't you just call go Crawl(url, depth-1, fetcher)
without the 2nd for loop and the done
channel? Is it because of the shared memory space for all the variables?
Thanks!