Daily Go language Bible-anonymous functions Exercise 2

Source: Internet
Author: User

Exercise 5.13: Modify the crawl so that it can save the discovered pages and, if necessary, create a table of contents to save the pages. Only the pages from the original domain name are saved. Assuming that the initial page is under golang.org, it is not
You want to save the page under Vimeo.com.

Package Mainimport ("FMT" "io" "Io/ioutil" "Links" "Log" "Net/http" "net /url "OS")///Exercise 5.13: Modify the crawl so that it can save the discovered pages and, if necessary, create a table of contents to save the pages. Only the pages from the original domain name are saved. If the initial page is under Golang.org, do not save the page under Vimeo.com. */var sum IntFunc Main () {Breadthfirst (crawl, OS. Args[1:])}/* crawl all connections to the page */func crawl (URL string) []string {sum++ go-save (URL) fmt. Printf ("%d|%s\n", Sum, URL) list, err: = links. Extract (URL) if err! = Nil {log. Print (Err)} return list}/* save page to file */func Save (U string) bool {Urlobj, _: = URL. Parse (u) path: = "/tmp/crawl/" + urlobj.host if Urlobj.path = = "" | |        Urlobj.path = = "/" {Urlobj.path = "/index.html"} filename: = Path + urlobj.path//Key Note file name Fmt. PRINTLN (filename)//Open File F, _: = OS. OpenFile (filename, os. O_create|os. O_append|os. O_RDWR, 0755)//Read link resp, geterr: = http. Get (u) if geterr! = Nil | | Resp. StatusCode! = http. Statusok {//resp. Body.close () return False} body, _: = Ioutil. ReadAll (resp. Body)//fmt. PRINTLN (body)//Create save Directory _, Err: = OS. Stat (PATH) if err! = nil {os. Mkdirall (Path, 0755)} io. WriteString (F, String (body)) resp. Body.close () BODY = nil return true}/* breadth first algorithm *///Breadthfirst calls F for each item in the worklist.//any I TEMs returned by F be added to the worklist.//F was called at most once for each Item.func Breadthfirst (f func (item Strin g) []string, worklist []string] {seen: = Make (Map[string]bool) for Len (worklist) > 0 {It EMS: = worklist worklist = Nil for _, Item: = Range Items {if!seen[ Item] {Seen[item] = True worklist = append (worklist, F (item                        )...)     }           }        }} 

  

Daily Go language Bible-anonymous functions Exercise 2

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.