AntoineAugusti / go-exercices

Exercices from the Go tour
30 stars 14 forks source link

Make goroutine synchronized? #2

Open u2386 opened 6 years ago

u2386 commented 6 years ago

https://github.com/AntoineAugusti/go-exercices/blob/f28113ebe9f41e12a6ea80da08dbc6d484f1763a/12-exercise-web-crawler.go#L53

New gopher. Is this block-channel makes goroutine synchronized?

Is this a better way?

children_quits := make(chan bool, len(urls))
for _, url := range urls {
    go RCrawl(url, depth-1, fetcher, children_quits)
}
for i := 0; i < len(urls); i++ {
    <-children_quits
}
lsdsjy commented 6 years ago

I think you are right. The original code is not concurrent. The for loop will not step until current RCrawl finishes, so there is only one goroutine running at any moment.