Created
January 17, 2017 20:37
-
-
Save dmitryzuev/b51df476ffff56f137407c5f5a4468cf to your computer and use it in GitHub Desktop.
A Tour of Go Exercise: Web Crawler
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" | |
"time" | |
) | |
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) | |
} | |
// Struct to handle results | |
type fetchedUrls struct { | |
urls map[string]*fetchedUrl | |
mux sync.Mutex | |
} | |
// Inner results url struct | |
type fetchedUrl struct { | |
body string | |
err error | |
} | |
// Add method to our fetched URLs | |
func (u *fetchedUrls) Add(url string, body string, err error) { | |
u.mux.Lock() | |
if _, ok := u.urls[url]; ok == false { | |
u.urls[url] = &fetchedUrl{body: body, err: err} | |
} | |
u.mux.Unlock() | |
} | |
// Method to check if url is present in our result | |
func (u *fetchedUrls) Check(url string) error { | |
u.mux.Lock() | |
if _, ok := u.urls[url]; ok == true { | |
defer u.mux.Unlock() | |
return fmt.Errorf("Already processed: %s", url) | |
} | |
defer u.mux.Unlock() | |
return nil | |
} | |
// Crawl also receives results variable | |
func Crawl(url string, depth int, fetcher Fetcher, fetched *fetchedUrls) { | |
if depth <= 0 { | |
return | |
} | |
// Check if we have url in our results | |
if fetched.Check(url) != nil { | |
return | |
} | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
// Add error to results | |
fetched.Add(url, "", err) | |
return | |
} | |
// Add fetched url to results | |
fetched.Add(url, body, nil) | |
for _, u := range urls { | |
// Perform another crawl asyncroniously | |
go Crawl(u, depth-1, fetcher, fetched) | |
} | |
return | |
} | |
func main() { | |
urls := fetchedUrls{urls: make(map[string]*fetchedUrl)} | |
// Run crawler in go routine | |
go Crawl("http://golang.org/", 4, fetcher, &urls) | |
// Right now I don't know how to handle go routines results without sleep timer | |
time.Sleep(time.Second) | |
// Display results | |
for k, url := range urls.urls { | |
if url.err != nil { | |
fmt.Println(url.err) | |
} else { | |
fmt.Printf("found %s %q\n", k, url.body) | |
} | |
} | |
} | |
// 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/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