Last active
November 24, 2022 10:40
-
-
Save Aadithya-V/938676639a0e9a4b262535a581e38ddc to your computer and use it in GitHub Desktop.
Go Tour Web Crawler Exercise Solution
This file contains 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" | |
"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 StringSet map[string]struct{} | |
type SafeStringSet struct{ | |
mu sync.Mutex | |
s StringSet | |
} | |
func (s StringSet) Has(url string) bool { | |
_, ok := s[url] | |
return ok | |
} | |
func (s StringSet) Add(url string) { | |
s[url] = struct{}{} | |
} | |
var urlCache = SafeStringSet{s: make(StringSet)} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
defer wg.Done() | |
if depth <= 0 { | |
return | |
} | |
urlCache.mu.Lock() | |
hasurl := urlCache.s.Has(url) | |
//Saves cpu cycles and uniquely adds to the set. Most common case. | |
urlCache.s.Add(url) | |
urlCache.mu.Unlock() | |
if !hasurl { | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, body) | |
for _, u := range urls { | |
wg.Add(1) | |
go Crawl(u, depth-1, fetcher) | |
} | |
} | |
return | |
} | |
var wg sync.WaitGroup | |
func main() { | |
wg.Add(1) | |
Crawl("https://golang.org/", 4, fetcher) | |
wg.Wait() | |
fmt.Println("Done") | |
} | |
// 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