Last active
August 29, 2015 14:27
-
-
Save alep/9f5e8812bd933045fe68 to your computer and use it in GitHub Desktop.
Web crawler from the tour of go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Fetcher interface { | |
// Fetch returns the body of URL and | |
// a slice of URLs found on that page. | |
Fetch(url string) (body string, urls []string, err error) | |
} | |
func PFetch(url string, ch chan []string) { | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
ch <- nil | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, body) | |
ch <- urls | |
} | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
// crawl that waits for the go routines to finish... | |
// suppose I break when depth == 0; the other go routines might not have | |
// finished before this function returns. In this case it's PFetch that | |
// prints the data, but if Crawl needed what PFetch send it might loose | |
if depth <= 0 { | |
return | |
} | |
var urls []string | |
var visited map[string]bool | |
var waiting_on int | |
ch := make(chan []string) | |
visited = make(map[string]bool) | |
go PFetch(url, ch) | |
waiting_on += 1 | |
visited[url] = true | |
depth -= 1 | |
for { | |
if depth <= 0 && waiting_on > 0 { | |
urls = <- ch | |
waiting_on -= 1 | |
continue | |
} else if waiting_on <= 0 { | |
break | |
} | |
urls = nil | |
for waiting_on > 0; waiting_on -- { | |
u := <- ch | |
if u == nil { | |
continue | |
} | |
append(urls, u...) | |
} | |
all := true | |
for _, u := range urls { | |
all = all && visited[u] | |
if !visited[u] { | |
go PFetch(u, depth, ch) | |
waiting_on += 1 | |
visited[u] = true | |
} | |
} | |
if all { | |
break | |
} | |
depth -= 1 | |
} | |
} | |
func main() { | |
Crawl("http://golang.org/", 4, fetcher) | |
} | |
// fakeFetcher is Fetcher that returns canned results. | |
type fakeFetcher map[string]*fakeResult | |
type fakeResult struct { | |
body string | |
urls []string | |
} | |
func (f fakeFetcher) Fetch(url string) (string, []string, error) { | |
if res, ok := f[url]; ok { | |
return res.body, res.urls, nil | |
} | |
return "", nil, fmt.Errorf("not found: %s", url) | |
} | |
// fetcher is a populated fakeFetcher. | |
var fetcher = fakeFetcher{ | |
"http://golang.org/": &fakeResult{ | |
"The Go Programming Language", | |
[]string{ | |
"http://golang.org/pkg/", | |
"http://golang.org/cmd/", | |
}, | |
}, | |
"http://golang.org/pkg/": &fakeResult{ | |
"Packages", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/cmd/", | |
"http://golang.org/pkg/fmt/", | |
"http://golang.org/pkg/os/", | |
}, | |
}, | |
"http://golang.org/pkg/fmt/": &fakeResult{ | |
"Package fmt", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/pkg/", | |
}, | |
}, | |
"http://golang.org/pkg/os/": &fakeResult{ | |
"Package os", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/pkg/", | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment