Last active
March 10, 2020 10:37
-
-
Save jcolson/81d6a30f1e0a86222f5e92077870b3bf to your computer and use it in GitHub Desktop.
my solution to tour.golang.org/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" | |
"sync" | |
) | |
// Fetcher interface | |
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) | |
} | |
// SafeVisitor to be used to determine if url has been visited | |
type SafeVisitor struct { | |
v map[string]bool | |
mux *sync.Mutex | |
} | |
var sv SafeVisitor = SafeVisitor{v: make(map[string]bool), mux: &sync.Mutex{}} | |
func (s SafeVisitor) checkvisited(url string) bool { | |
s.mux.Lock() | |
defer s.mux.Unlock() | |
_, ok := s.v[url] | |
if ok == false { | |
s.v[url] = true | |
} | |
return ok | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher, wg *sync.WaitGroup) { | |
defer wg.Done() | |
if depth <= 0 || sv.checkvisited(url) { | |
return | |
} | |
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, wg) | |
} | |
return | |
} | |
func main() { | |
var wg sync.WaitGroup | |
wg.Add(1) | |
Crawl("https://golang.org/", 4, fetcher, &wg) | |
// Wait for all Crawls to complete | |
wg.Wait() | |
} | |
// 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