Trouble with go tour crawler exercise

Posted by David Mason on Stack Overflow See other posts from Stack Overflow or by David Mason
Published on 2012-11-04T09:49:22Z Indexed on 2012/11/04 23:00 UTC
Read the original article Hit count: 148

Filed under:

I'm going through the go tour and I feel like I have a pretty good understanding of the language except for concurrency.

On slide 71 there is an exercise that asks the reader to parallelize a web crawler (and to make it not cover repeats but I haven't gotten there yet.)

Here is what I have so far:

func Crawl(url string, depth int, fetcher Fetcher, ch chan string) {
    if depth <= 0 {
        return
    }

    body, urls, err := fetcher.Fetch(url)
    if err != nil {
        ch <- fmt.Sprintln(err)
        return
    }

    ch <- fmt.Sprintf("found: %s %q\n", url, body)
    for _, u := range urls {
        go Crawl(u, depth-1, fetcher, ch)
    }
}

func main() {
    ch := make(chan string, 100)
    go Crawl("http://golang.org/", 4, fetcher, ch)

    for i := range ch {
        fmt.Println(i)
    }
}

The issue I have is where to put the close(ch) call. If I put a defer close(ch) somewhere in the Crawl method, then I end up writing to a closed channel in one of the spawned goroutines, since the method will finish execution before the spawned goroutines do.

If I omit the call to close(ch), as is shown in my example code, the program deadlocks after all the goroutines finish executing but the main thread is still waiting on the channel in the for loop since the channel was never closed.

© Stack Overflow or respective owner

Related posts about go

  • Go import error while trying to import web.go package after using goinstall

    as seen on Stack Overflow - Search for 'Stack Overflow'
    With halfdans advice, I was successfully able to use goinstall github.com/hoisie/web.go without any errors after installing git first. However, now when I try to compile the sample code given, go is not finding the web package. I get the error, main.go:4: can't find import: web On this code package… >>> More

  • Go Big or Go Home

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    The Oracle Develop conference (#oracledevelop10), being co-located for the first time ever with JavaOne in San Francisco, is guaranteed to be the ultimate rush for developers this year. Where else can you go to learn about, interact with, and meet fellow devotees of the entire Oracle Development… >>> More

  • Go Big or Go Special

    as seen on SQL Team - Search for 'SQL Team'
    Watching Shark Tank tonight and the first presentation was by Mango Mango Preserves and it highlighted an interesting contrast in business trends today and how to capitalize on opportunities.  <Spoiler Alert> Even though every one of the sharks was raving about the product samples they tried… >>> More

  • Go Big or Go Home

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    For those who don’t know, Oracle sponsors a group called “OWL” – Oracle Women’s Leadership - and the purpose of the group is to create local and global opportunities that support, educate and empower current and future women leaders at Oracle. This week, I had the opportunity to attend the Denver… >>> More

  • juju bootstrap fails with a local environment, why?

    as seen on Ask Ubuntu - Search for 'Ask Ubuntu'
    Each time I try to bootstrap juju using a local enviroment it fails starting the juju-db-braiam-local script as follows: $ sudo juju --debug --verbose bootstrap 2013-10-20 02:28:53 INFO juju.provider.local environprovider.go:32 opening environment "local" 2013-10-20 02:28:53 DEBUG juju.provider.local… >>> More