GOPL中的代码:并发Web搜寻器

Here is an example code from the book "The Go Programming Language" by Donovan and Kernighan. It's about a simple concurrent web crawler.

https://github.com/adonovan/gopl.io/blob/master/ch8/crawl3/findlinks.go

When I put this part (in the main function)

seen := make(map[string]bool)
for list := range worklist {
    for _, link := range list {
           ....
        }
    }
}

in front of

for i := 0; i < 20; i++ {
    go func() {
        for link := range unseenLinks {
          ...
        }
    }()
}

the code does not work. Why is this?

If you swap those - for list := range worklist { line is executed before you start goroutine.

And because worklist is a non-closed empty channel - you're blocked there forever.

Whereas, while they are in the right order - goroutines send to the worklist and your main thread reads from it.