Go exercise: Web crawler

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Code:

Saferun Lock Setting URL visited

Pass

For i:=0; I < Len (URLs); I++{<-sub_quit}
Parent go thread waits for child go thread to end

Package Mainimport ("FMT") type Fetcher interface {//Fetch returns the body content of the URL and places the URL found on this page into a slice. Fetch (URL string) (body string, URLs []string, err error)}var Lockx = Make (chan int,1) func Saferun (f func ()) {&LT;-LOCKXF ( ) Lockx <-1}var visited Map[string]bool = Make (Map[string]bool)//Crawl use Fetcher to crawl the page recursively from a URL until the maximum depth is reached. Func Crawl (URL string, depth int, fetcher fetcher, quit Chan int) {//TODO: Crawl URL in parallel.    TODO: Do not repeat crawl pages. Neither of the following is true: Defer func () {quit<-1} () If depth <= 0 | | Visited[url] {return}body, URLs, err: = Fetcher. Fetch (URL) saferun (func () {visited[url]=true}) if err! = Nil {fmt. PRINTLN (Err) return}fmt. Printf ("Found:%s%q\n", url, body) Sub_quit: = Make (chan Int,len (URLs)) for _, U: = Range URLs {Crawl (U, depth-1, fetcher,s Ub_quit)}for i:=0; I < Len (URLs); I++{<-sub_quit}return}func Main () {lockx<-1quit: = Make (chan int,1) Crawl ("http://golang.org/", 4, Fetcher,quit) Fmt. Println (<-quit)}//Fakefetcher is a fetcher that returns several results. Type Fakefetcher MAP[STRIng]*fakeresulttype fakeresult struct {body Stringurls []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/",},},}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.