Created
November 4, 2020 05:52
-
-
Save morishin/2e94a200ba65ab3ec03c66fb4c00db7c to your computer and use it in GitHub Desktop.
My answer for: https://go-tour-jp.appspot.com/concurrency/10
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" | |
"time" | |
) | |
type Fetcher interface { | |
// Fetch returns the body of URL and | |
// a slice of URLs found on that page. | |
Fetch(url string) FetchResult | |
} | |
type FetchResult struct { | |
body string | |
urls []string | |
err error | |
} | |
type Crawler struct { | |
fetcher Fetcher | |
results map[string]chan FetchResult | |
} | |
func (crawler *Crawler) Crawl(url string, depth int) { | |
if depth <= 0 { | |
return | |
} | |
if _, ok := crawler.results[url]; ok { | |
// do nothing | |
} else { | |
crawler.results[url] = make(chan FetchResult, 1) | |
crawler.results[url] <- crawler.fetcher.Fetch(url) | |
} | |
result := <-crawler.results[url] | |
if result.err != nil { | |
fmt.Println(result.err) | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, result.body) | |
for _, u := range result.urls { | |
go crawler.Crawl(u, depth-1) | |
} | |
return | |
} | |
func main() { | |
crawler := Crawler{fetcher, map[string]chan FetchResult{}} | |
crawler.Crawl("https://golang.org/", 4) | |
time.Sleep(50 * time.Millisecond) | |
} | |
// 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) FetchResult { | |
if res, ok := f[url]; ok { | |
return FetchResult{res.body, res.urls, nil} | |
} | |
return FetchResult{"", nil, fmt.Errorf("not found: %s", url)} | |
} | |
// fetcher is a populated fakeFetcher. | |
var fetcher = fakeFetcher{ | |
"https://golang.org/": &fakeResult{ | |
"The Go Programming Language", | |
[]string{ | |
"https://golang.org/pkg/", | |
"https://golang.org/cmd/", | |
}, | |
}, | |
"https://golang.org/pkg/": &fakeResult{ | |
"Packages", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/cmd/", | |
"https://golang.org/pkg/fmt/", | |
"https://golang.org/pkg/os/", | |
}, | |
}, | |
"https://golang.org/pkg/fmt/": &fakeResult{ | |
"Package fmt", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/pkg/", | |
}, | |
}, | |
"https://golang.org/pkg/os/": &fakeResult{ | |
"Package os", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/pkg/", | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment