// soleTitle returns the text of the first non-empty title element// in doc, and an error if there was not exactly one.funcsoleTitle(doc*html.Node)(titlestring,errerror){typebailoutstruct{}deferfunc(){switchp:=recover();p{casenil:// no paniccasebailout{}:// "expected" panicerr=fmt.Errorf("multiple title elements")default:panic(p)// unexpected panic; carry on panicking}}()// Bail out of recursion if we find more than one nonempty title.forEachNode(doc,func(n*html.Node){ifn.Type==html.ElementNode&&n.Data=="title"&&n.FirstChild!=nil{iftitle!=""{panic(bailout{})// multiple titleelements}title=n.FirstChild.Data}},nil)iftitle==""{return"",fmt.Errorf("no title element")}returntitle,nil}