Last active
August 29, 2015 14:08
-
-
Save batkinson/8cfa6ea7516e7f496e01 to your computer and use it in GitHub Desktop.
Go language tour #73 solution
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(url string) (body string, urls []string, err error) | |
} | |
type FetchResult struct { | |
body string | |
urls []string | |
depth int | |
} | |
func FetchUrl(url string, depth int, resultc chan <- FetchResult) { | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
resultc <- FetchResult{ body: "", urls: []string{}, depth: depth-1 } | |
} else { | |
fmt.Printf("found: %s %q\n", url, body) | |
resultc <- FetchResult{ body: body, urls: urls, depth: depth-1 } | |
} | |
} | |
func Crawl(url string, depth int) { | |
results := make(chan FetchResult) | |
visited := make(map[string] bool) | |
go FetchUrl(url, depth, results) | |
outstanding := 1 | |
visited[url] = true | |
for outstanding > 0 { | |
result := <-results | |
outstanding-- | |
if result.depth > 0 { | |
for _, url := range result.urls { | |
if visited[url] != true { | |
go FetchUrl(url, result.depth, results) | |
outstanding++ | |
visited[url] = true | |
} | |
} | |
} | |
} | |
} | |
func main() { | |
Crawl("http://golang.org/", 4) | |
} | |
// 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/fmt/en", | |
"http://golang.org/pkg/fmt/fr", | |
"http://golang.org/pkg/fmt/es", | |
}, | |
}, | |
"http://golang.org/pkg/fmt/en": &fakeResult{ | |
"Package English", | |
[]string { | |
"http://golang.org/pkg/fmt/en/pineapple", | |
}, | |
}, | |
"http://golang.org/pkg/fmt/fr": &fakeResult{ | |
"Package French", | |
[]string { | |
"http://golang.org/pkg/fmt/en/ananas", | |
}, | |
}, | |
"http://golang.org/pkg/fmt/es": &fakeResult{ | |
"Package Español", | |
[]string { | |
"http://golang.org/pkg/fmt/en/piña", | |
}, | |
}, | |
"http://golang.org/pkg/fmt/en/pineapple": &fakeResult{ | |
"Package Pineapple", | |
[]string { | |
}, | |
}, | |
"http://golang.org/pkg/fmt/en/ananas": &fakeResult{ | |
"Package Ananas", | |
[]string { | |
}, | |
}, | |
"http://golang.org/pkg/fmt/en/piña": &fakeResult{ | |
"Package Piña", | |
[]string { | |
}, | |
}, | |
"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