Created
September 20, 2018 19:40
-
-
Save zwhitchcox/fdbf0026bb965e62c44172ce63bb9339 to your computer and use it in GitHub Desktop.
Go Exercise: Web Crawler
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(url string) (body string, urls []string, err error) | |
} | |
func (c *Crawling) AddCount() { | |
c.mux.Lock() | |
c.count++ | |
c.mux.Unlock() | |
} | |
func (c *Crawling) SubCount() { | |
c.mux.Lock() | |
c.count-- | |
if c.count <= 0 { | |
c.quit <- true | |
} | |
c.mux.Unlock() | |
} | |
type Crawling struct { | |
visited map[string]bool | |
mux sync.Mutex | |
count int | |
quit chan bool | |
} | |
func (c *Crawling) AddUrl(url string) { | |
c.mux.Lock() | |
c.visited[url] = true | |
c.mux.Unlock() | |
} | |
func (c *Crawling) Crawl(url string, fetcher Fetcher) { | |
body, urls, err := fetcher.Fetch(url) | |
c.AddUrl(url) | |
if err != nil { | |
fmt.Println(err) | |
c.SubCount() | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, body) | |
for _, u := range urls { | |
if !c.visited[u] { | |
c.AddCount() | |
go c.Crawl(u, fetcher) | |
} | |
} | |
c.SubCount() | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
quit := make(chan bool) | |
crawling_urls := Crawling{visited: make(map[string]bool), quit: quit, count: 1} | |
go crawling_urls.Crawl(url, fetcher) | |
<- quit | |
} | |
func main() { | |
Crawl("https://golang.org/", 4, fetcher) | |
} | |
// 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