On Sun, Aug 20, 2017 at 08:20:08AM -0700, 823545...@qq.com wrote:

> I'm a freshman. I went through the Tour of GO smoothly today, until I 
> encountered "Exercise: Web Crawler". The exercise is at here 
> https://tour.golang.org/concurrency/10 .
> This exercise requires me to implement a concurrent crawler. I write a main 
> function like this:
>  
> 
> > func main() {
> >     go Crawl("http://golang.org/";, 4, fetcher)
> > }
> 
> 
> Yes, the program terminated just after it created the goroutine, nothing 
> was printed. I thought the program would wait for all goroutines to finish 
> without explicit coding, but it didn't. The canonical way to do this is to 
> use WaitGroup, but the tutorial omitted it. I think it should be added 
> before this exercise.

Supposedly the authors implied you'd use a channel to have individual
crawlers run on separate goroutines to communicate the results of their
work back to the controlling goroutine (the main one) via a channel.

(I also think the "Don't fetch the same URL twice" invariant is supposed
to be kept through using the sync.Mutex introduced in exercise 9).

The question of where to use sync.WaitGroup and where to synchronize
using channels (or sync.Cond or whatever else) is both technical and
philosophical.  Dumping it right onto the heads of freshmen would be
nonsensical, and since channels are viewed as the most natural and
elegant way of making the concurrent workers synchronize their work,
that's what typically gets taught first.

I heartily recommend you to read [1] and then [2], in this order.
[1] is one of the best-written books on CS matters I ever read, and it
introduces concurrency in Go pretty gently.
[2] is more hard-core (as it focuses precisely on concurrency matters),
it's less smooth in presenting its material than the first book, but it
gets its job done well and is definitely worth its price IMO.

1. http://www.gopl.io/
2. http://katherine.cox-buday.com/concurrency-in-go/

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to