Skip to content

Instantly share code, notes, and snippets.

@dlintw
Created October 11, 2011 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlintw/1278147 to your computer and use it in GitHub Desktop.
Save dlintw/1278147 to your computer and use it in GitHub Desktop.
go tour #69
func Crawl(url string, depth, workerCnt int, fetcher Fetcher) {
reqch := make(chan int)
seen := make(map[string]bool)
working := make(chan bool, workerCnt)
var crawl func(string, int)
crawl = func(url string, depth int) {
defer func(){reqch <- -1}()
if depth <= 0 {
return
}
if _,ok := seen[url] ; ok {
return
}
seen[url] = true
working <- true
defer func(){ <-working}()
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("found: %s %q\n", url, body)
reqch <- len(urls)
for _, u := range urls {
go crawl(u, depth-1)
}
}
go crawl(url, depth)
actsum := 1
for diff :=range reqch {
actsum += diff
if actsum == 0 {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment