Skip to content

Instantly share code, notes, and snippets.

@e1himself
Created January 31, 2015 20:31
Show Gist options
  • Save e1himself/caf9e937ecb0d861daa2 to your computer and use it in GitHub Desktop.
Save e1himself/caf9e937ecb0d861daa2 to your computer and use it in GitHub Desktop.
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)
}
// We put these objects into channel, empty task denotes sequence end
type FetchTask struct {
url string
depth int
}
func generate_tasks(task FetchTask, fettcher Fetcher, out chan FetchTask) {
defer func () { out <- FetchTask{} }() // Put empty task to channel: task generation sequance is complete
if task.depth <= 0 {
return
}
body, urls, err := fetcher.Fetch(task.url)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("found: %s %q\n", task.url, body)
for _, u := range urls {
out <- FetchTask{u, task.depth-1} // put new fetch task to channel
}
return
}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher) {
// Mark all visited urls
visited := make(map[string]bool)
// tasks queue
out := make(chan FetchTask)
// count how many task generators are active
num := 1
// mark first url as visited and go
visited[url] = true
go generate_tasks(FetchTask{url, depth}, fetcher, out)
for {
// wait for next task from channel
task := <-out
switch {
// empty task: a task generator ended
case task.url == "":
num-- // decrease generators number
if num == 0 { // exit all generators are complete
return
}
case visited[task.url] == false: // if we have task with unvisited url
visited[task.url] = true // mark as visited
num++ // increase generators number
go generate_tasks(task, fetcher, out) // start task generator
}
}
}
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/",
},
},
}
found: http://golang.org/ "The Go Programming Language"
found: http://golang.org/pkg/ "Packages"
not found: http://golang.org/cmd/
found: http://golang.org/pkg/fmt/ "Package fmt"
found: http://golang.org/pkg/os/ "Package os"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment