Skip to content

Instantly share code, notes, and snippets.

@reeFridge
Created May 2, 2019 17:44
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 reeFridge/99147164dec229b0b7c9f14a51972fa3 to your computer and use it in GitHub Desktop.
Save reeFridge/99147164dec229b0b7c9f14a51972fa3 to your computer and use it in GitHub Desktop.
Solution for Web Crawler exercise of Go's Tour
package main
import (
"fmt"
"sync"
)
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)
}
type urlCache map[string]*fakeResult
// Safe-n-Cached wrapper for Fetcher
type cachedFetcher struct {
mutex sync.Mutex
cache urlCache
fetcher Fetcher
}
func (self *cachedFetcher) Fetch(url string) (string, []string, error) {
defer self.mutex.Unlock()
self.mutex.Lock()
if res, ok := self.cache[url]; ok {
return res.body, res.urls, nil
}
body, urls, err := fetcher.Fetch(url)
if err == nil {
self.cache[url] = &fakeResult{
"[cached] " + body,
urls,
}
}
return body, urls, err
}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher, resultChan chan string) {
// This implementation doesn't do either:
if depth <= 0 {
return
}
body, urls, err := fetcher.Fetch(url)
if err != nil {
resultChan <- fmt.Sprintf("%s", err)
return
}
resultChan <- fmt.Sprintf("found: %s %q", url, body)
var wg sync.WaitGroup
for _, u := range urls {
wg.Add(1)
// try to remove passing url to the closure :)
go func(url string) {
defer wg.Done()
Crawl(url, depth-1, fetcher, resultChan)
}(u)
}
wg.Wait()
}
func main() {
resultChan := make(chan string)
go func() {
Crawl("https://golang.org/", 4, &cachedFetcher{
cache: make(urlCache),
fetcher: &fetcher,
}, resultChan)
close(resultChan)
}()
for result := range resultChan {
fmt.Println(result)
}
}
// 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{
"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