MCPcopy
hub / github.com/adonovan/gopl.io / title

Function title

ch5/title3/title.go:67–91  ·  view source on GitHub ↗

!-

(url string)

Source from the content-addressed store, hash-verified

65//!-
66
67func title(url string) error {
68 resp, err := http.Get(url)
69 if err != nil {
70 return err
71 }
72
73 // Check Content-Type is HTML (e.g., "text/html; charset=utf-8").
74 ct := resp.Header.Get("Content-Type")
75 if ct != "text/html" && !strings.HasPrefix(ct, "text/html;") {
76 resp.Body.Close()
77 return fmt.Errorf("%s has type %s, not text/html", url, ct)
78 }
79
80 doc, err := html.Parse(resp.Body)
81 resp.Body.Close()
82 if err != nil {
83 return fmt.Errorf("parsing %s as HTML: %v", url, err)
84 }
85 title, err := soleTitle(doc)
86 if err != nil {
87 return err
88 }
89 fmt.Println(title)
90 return nil
91}
92
93func main() {
94 for _, arg := range os.Args[1:] {

Callers 1

mainFunction · 0.70

Calls 3

soleTitleFunction · 0.85
GetMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected