Skip to content

Instantly share code, notes, and snippets.

@hanshsieh
Created January 19, 2019 14:18
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 hanshsieh/7e011a7ca53c1615483ed11503ae5471 to your computer and use it in GitHub Desktop.
Save hanshsieh/7e011a7ca53c1615483ed11503ae5471 to your computer and use it in GitHub Desktop.
Go Tour Exercise
package main
// Exercise: Web Crawler
// https://tour.golang.org/concurrency/10
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)
}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher, urlStorage *UrlStorage, urlChannel chan string) {
// TODO: Fetch URLs in parallel.
// TODO: Don't fetch the same URL twice.
// This implementation doesn't do either:
defer func() {
urlChannel <- url
}()
if depth <= 0 {
return
}
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("found: %s %q\n", url, body)
subUrlChannel := make(chan string, len(urls))
validUrl := 0
for _, u := range urls {
if !urlStorage.AddUrl(u) {
continue
}
validUrl += 1
go Crawl(u, depth-1, fetcher, urlStorage, subUrlChannel)
}
for i := 0; i < validUrl; i += 1 {
url := <- subUrlChannel
fmt.Printf("Finish process url %s", url)
}
return
}
type UrlStorage struct {
urls map[string]bool
mutex sync.Mutex
}
func NewUrlStorage() *UrlStorage {
return &UrlStorage{urls: make(map[string]bool, 1000)}
}
func (this *UrlStorage) AddUrl(url string) bool {
this.mutex.Lock()
defer this.mutex.Unlock()
if _, hasUrl := this.urls[url]; hasUrl {
return false
}
this.urls[url] = true
return true
}
func main() {
urlStorage := NewUrlStorage()
urlChannel := make(chan string, 1)
Crawl("https://golang.org/", 4, fetcher, urlStorage, urlChannel)
}
// 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