day8方法
parent
2e765820cd
commit
62a52095d1
|
@ -24,7 +24,7 @@ func title(url string) error {
|
||||||
}
|
}
|
||||||
// 直到包含defer的函数执行完毕之后,defer的这条语句才会执行
|
// 直到包含defer的函数执行完毕之后,defer的这条语句才会执行
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
// Check Content-Type is HTML (e.g., "text/html;charset=utf-8").
|
// Check Content-Type is Html (e.g., "text/html;charset=utf-8").
|
||||||
ct := resp.Header.Get("Content-Type")
|
ct := resp.Header.Get("Content-Type")
|
||||||
if ct != "text/html" && !strings.HasPrefix(ct, "text/html;") {
|
if ct != "text/html" && !strings.HasPrefix(ct, "text/html;") {
|
||||||
return fmt.Errorf("%s has type %s, not text/html", url, ct)
|
return fmt.Errorf("%s has type %s, not text/html", url, ct)
|
||||||
|
@ -32,7 +32,7 @@ func title(url string) error {
|
||||||
doc, err := html.Parse(resp.Body)
|
doc, err := html.Parse(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parsing %s as HTML: %v", url, err)
|
return fmt.Errorf("parsing %s as Html: %v", url, err)
|
||||||
}
|
}
|
||||||
visitNode := func(n *html.Node) {
|
visitNode := func(n *html.Node) {
|
||||||
if n.Type == html.ElementNode && n.Data == "title" && n.FirstChild != nil {
|
if n.Type == html.ElementNode && n.Data == "title" && n.FirstChild != nil {
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type www interface {
|
||||||
|
Write()
|
||||||
|
}
|
||||||
|
type ByteCounter int
|
||||||
|
|
||||||
|
func (c *ByteCounter) Write(p []byte) (int, error) {
|
||||||
|
*c += ByteCounter(len(p))
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var c ByteCounter
|
||||||
|
c.Write([]byte("hello"))
|
||||||
|
fmt.Println(c)
|
||||||
|
name := "dugulp"
|
||||||
|
fmt.Fprintf(&c, "Hello, %s", name)
|
||||||
|
fmt.Println(c)
|
||||||
|
}
|
Loading…
Reference in New Issue