packagetitleimport("golang.org/x/net/html""io")funcisTitleElement(n*html.Node)bool{returnn.Type==html.ElementNode&&n.Data=="title"}functraverse(n*html.Node)(string,bool){ifisTitleElement(n){returnn.FirstChild.Data,true}forc:=n.FirstChild;c!=nil;c=c.NextSibling{result,ok:=traverse(c)ifok{returnresult,ok}}return"",false}funcGetHtmlTitle(rio.Reader)(string,bool){doc,err:=html.Parse(r)iferr!=nil{panic("Fail to parse html")}returntraverse(doc)}
packagetitleimport("net/http""testing")funcTestHtmlToRst(t*testing.T){resp,err:=http.Get("http://nanda.online-dhamma.net/")//resp, err := http.Get("https://siongui.github.io/zh/2016/03/14/pillow-useful-items-for-me-notes/")iferr!=nil{panic(err)}deferresp.Body.Close()iftitle,ok:=GetHtmlTitle(resp.Body);ok{println(title)}else{println("Fail to get HTML title")}}