Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8473637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:24:21+00:00 2026-06-10T17:24:21+00:00

I am going through the golang tour and working on the final exercise to

  • 0

I am going through the golang tour and working on the final exercise to change a web crawler to crawl in parallel and not repeat a crawl ( http://tour.golang.org/#73 ). All I have changed is the crawl function.

    var used = make(map[string]bool)

    func Crawl(url string, depth int, fetcher Fetcher) {
        if depth <= 0 {
            return
        }
        body, urls, err := fetcher.Fetch(url)
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Printf("\nfound: %s %q\n\n", url, body)
        for _,u := range urls {
            if used[u] == false {
                used[u] = true
                Crawl(u, depth-1, fetcher)
            }
        }
        return
    }

In order to make it concurrent I added the go command in front of the call to the function Crawl, but instead of recursively calling the Crawl function the program only finds the “http://golang.org/” page and no other pages.

Why doesn’t the program work when I add the go command to the call of the function Crawl?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:24:23+00:00Added an answer on June 10, 2026 at 5:24 pm

    The problem seems to be, that your process is exiting before all URLs can be followed
    by the crawler. Because of the concurrency, the main() procedure is exiting before
    the workers are finished.

    To circumvent this, you could use sync.WaitGroup:

    func Crawl(url string, depth int, fetcher Fetcher, wg *sync.WaitGroup) {
        defer wg.Done()
        if depth <= 0 {
             return
        }
        body, urls, err := fetcher.Fetch(url)
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Printf("\nfound: %s %q\n\n", url, body)
        for _,u := range urls {
            if used[u] == false {
               used[u] = true
               wg.Add(1)
               go Crawl(u, depth-1, fetcher, wg)
            }
        }
        return
    }
    

    And call Crawl in main as follows:

    func main() {
        wg := &sync.WaitGroup{}
    
        Crawl("http://golang.org/", 4, fetcher, wg)
    
        wg.Wait()
    }
    

    Also, don’t rely on the map being thread safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im going through the setup on http://www.railstutorial.org/chapters/static-pages#fig:autotest_green and im stuck on this particular error:
Going through the Game Center / GameKit docs, I could not find any information
After going through some examples on the web I realize that there is a
Going through the tutorial here: http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs I've noticed that they are passing the EF
While going through the last edits of the C++0x Working draft I found a
After going through the build process (found at: http://source.android.com/source/building.html ) for the master branch
Going through D3.js tutorials, I see two different conventions, and I am not sure
Going through this tutorial https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC it is mentioned that to set up a call
Going through happstack-lite tutorial : we build functions that have return type of ServerPart
Going through some of my older Delphi projects and upgrading them to D2009, as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.