Last active
May 10, 2025 19:57
-
-
Save martin-mok/a86af8227a858a8ffc1874b953f9aeeb to your computer and use it in GitHub Desktop.
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
// Reference: https://github.com/golang/tour/blob/release-branch.go1.4/solutions/webcrawler.go | |
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) | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
var fetched = struct { | |
m map[string]bool | |
sync.Mutex | |
}{m: make(map[string]bool)} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
if depth <= 0 { | |
fmt.Printf("<- Done with %v, depth 0.\n", url) | |
return | |
} | |
fetched.Lock() | |
if _, ok := fetched.m[url]; ok { | |
fetched.Unlock() | |
fmt.Printf("<- Done with %v, already fetched.\n", url) | |
return | |
} | |
// We mark the url to be fetching to avoid others fetching it at the same time. | |
fetched.m[url] = true | |
fetched.Unlock() | |
// We load it concurrently. | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Printf("<- Error on %v: %v\n", url, err) | |
return | |
} | |
fmt.Printf("Found: %s %q\n", url, body) | |
done := make(chan bool) | |
for i, u := range urls { | |
fmt.Printf("-> Crawling child %v/%v of %v : %v.\n", i, len(urls), url, u) | |
go func(url string) { | |
Crawl(url, depth-1, fetcher) | |
done <- true | |
}(u) | |
} | |
for i, u := range urls { | |
fmt.Printf("<- [%v] %v/%v Waiting for child %v.\n", url, i, len(urls), u) | |
<-done | |
} | |
fmt.Printf("<- Done with %v\n", url) | |
} | |
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