Last active
August 29, 2015 14:01
-
-
Save delta2323/4e45fad26eb9269ab5e7 to your computer and use it in GitHub Desktop.
Solution for "A Tour of Go" Exercise #73 (This code does NOT work properly)
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
/* | |
Solution for "A Tour of Go" Exercise #73 (This code does NOT work properly) | |
http://tour.golang.org/#73 | |
*/ | |
package main | |
import ( | |
"fmt" | |
) | |
type Set map[interface{}]bool | |
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) | |
} | |
func DoCrawl2(url string, depth int, fetcher Fetcher, crawled Set, ch chan string, errch chan error) { | |
if depth <= 0 { | |
return | |
} | |
if crawled[url] { | |
return | |
} | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
errch <- err | |
return | |
} | |
crawled[url] = true | |
ch <- fmt.Sprintf("found: %s %q", url, body) | |
child_chs := make([]chan string, len(urls)) | |
child_errchs := make([]chan error, len(urls)) | |
for i, u := range urls { | |
child_chs[i] = make(chan string) | |
child_errchs[i] = make(chan error) | |
go DoCrawl(u, depth-1, fetcher, crawled, child_chs[i], child_errchs[i]) | |
} | |
for _, c := range child_chs { | |
for u := range c { | |
ch <- u | |
} | |
} | |
for _, c := range child_errchs { | |
for e := range c { | |
errch <- e | |
} | |
} | |
return | |
} | |
func DoCrawl(url string, depth int, fetcher Fetcher, crawled Set, ch chan string, errch chan error) { | |
DoCrawl2(url, depth, fetcher, crawled, ch, errch) | |
close(ch) | |
close(errch) | |
} | |
func Crawl(url string, depth int, fetcher Fetcher, ch chan string, errch chan error) { | |
crawled := make(Set) | |
DoCrawl(url, depth, fetcher, crawled, ch, errch) | |
} | |
func main() { | |
ch, errch := make(chan string), make(chan error) | |
go Crawl("http://golang.org/", 4, fetcher, ch, errch) | |
fmt.Println("Fetched:") | |
for x := range ch { | |
fmt.Println(x) | |
for x := range ch { | |
fmt.Println(x) | |
} | |
fmt.Println("Error:") | |
for x := range errch { | |
fmt.Println(x.Error()) | |
} | |
} | |
// 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