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