编辑代码

package main
import "fmt"
func main () {
	fmt.Println("Hello JSRUN!   \n\n         - from Golang .")
}

func title( url string  ) error{
    resp ,err  := http.Get(url)
    if err != nil {
        return err
    }

    ct := resp.Header.Get("Content Type")
    if ct != "text/html" && !strings.HasPrefix(ct,"text/html"){
        resp.Body.Close()
        return fmt.Error("%s has type %s ,not text/html",url,ct)
    }

    doc ,err := html.Parse(resp.Body)
    resp.Body.Close()
    if err != nil{
        return fmt.Errorf("parsing %s as Html:%v",url,err)_
    }

    visitNode := func(n *html.Node){
        if n.Type == html.ElementNode && n.Data == "title" && n.FirstChild != nil{
            fmt.Println(n.FirstChild.Data)
        }
    }
    forEachNode(doc,visitNode,nil)
    return nil
}