funcmain(){for_,url:=rangeos.Args[1:]{links,err:=findLinks(url)iferr!=nil{fmt.Fprintf(os.Stderr,"findlinks2: %v\n",err)continue}for_,link:=rangelinks{fmt.Println(link)}}}// findLinks performs an HTTP GET request for url, parses the// response as HTML, and extracts and returns the links.funcfindLinks(urlstring)([]string,error){resp,err:=http.Get(url)iferr!=nil{returnnil,err}ifresp.StatusCode!=http.StatusOK{resp.Body.Close()returnnil,fmt.Errorf("getting %s: %s",url,resp.Status)}doc,err:=html.Parse(resp.Body)resp.Body.Close()iferr!=nil{returnnil,fmt.Errorf("parsing %s as HTML: %v",url,err)}returnvisit(nil,doc),nil}
// CountWordsAndImages does an HTTP GET request for the HTML
// document url and returns the number of words and images in it.
func CountWordsAndImages(url string) (words, images int, err error) {
resp, err := http.Get(url)
if err != nil {
return
}
doc, err := html.Parse(resp.Body)
resp.Body.Close()
if err != nil {
err = fmt.Errorf("parsing HTML: %s", err)
return
}
words, images = countWordsAndImages(doc)
return
}
func countWordsAndImages(n *html.Node) (words, images int) { /* ... */ }