-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (62 loc) · 1.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"bytes"
"fmt"
"os"
"strings"
"time"
"golang.org/x/net/html"
)
const rawHtml = `
<!DOCTYPE html>
<html>
<body>
<h1> one two three four five is an example</h1>
<p>Some more words to count</p>
<img src="https://golang.org/doc/gopher/frontpage.png" />
</body>
</html>
`
func visitNode(n *html.Node, words, images *int) {
if n.Type == html.TextNode {
*words += len(strings.Fields(n.Data))
} else if n.Type == html.ElementNode && n.Data == "img" {
*images += 1
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
visitNode(c, words, images)
}
}
func countWordsAndImages(doc *html.Node) (int, int) {
var words, images int
visitNode(doc, &words, &images)
return words, images
}
// structs
type Employee struct {
Name string
Number int
Boss *Employee
Hired time.Time
}
func main() {
root, err := html.Parse(bytes.NewReader([]byte(rawHtml)))
if err != nil {
fmt.Fprintf(os.Stderr, "Site is not valid HTML: %v", err)
os.Exit(1)
}
words, pics := countWordsAndImages(root)
fmt.Printf("%d words and %d images\n", words, pics)
// ex2
var album = struct {
title string
artist string
year int
copies int
}{
"The White Album",
"The Beatles",
1968,
1000,
}
}