mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-11-27 21:01:51 +00:00
fmt
This commit is contained in:
100
ch5/ch5-02.md
100
ch5/ch5-02.md
@@ -11,25 +11,25 @@ golang.org/x/net/html
|
||||
package html
|
||||
|
||||
type Node struct {
|
||||
Type NodeType
|
||||
Data string
|
||||
Attr []Attribute
|
||||
FirstChild, NextSibling *Node
|
||||
Type NodeType
|
||||
Data string
|
||||
Attr []Attribute
|
||||
FirstChild, NextSibling *Node
|
||||
}
|
||||
|
||||
type NodeType int32
|
||||
|
||||
const (
|
||||
ErrorNode NodeType = iota
|
||||
TextNode
|
||||
DocumentNode
|
||||
ElementNode
|
||||
CommentNode
|
||||
DoctypeNode
|
||||
ErrorNode NodeType = iota
|
||||
TextNode
|
||||
DocumentNode
|
||||
ElementNode
|
||||
CommentNode
|
||||
DoctypeNode
|
||||
)
|
||||
|
||||
type Attribute struct {
|
||||
Key, Val string
|
||||
Key, Val string
|
||||
}
|
||||
|
||||
func Parse(r io.Reader) (*Node, error)
|
||||
@@ -43,21 +43,21 @@ gopl.io/ch5/findlinks1
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
func main() {
|
||||
doc, err := html.Parse(os.Stdin)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "findlinks1: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
for _, link := range visit(nil, doc) {
|
||||
fmt.Println(link)
|
||||
}
|
||||
doc, err := html.Parse(os.Stdin)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "findlinks1: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
for _, link := range visit(nil, doc) {
|
||||
fmt.Println(link)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -66,17 +66,17 @@ visit函數遍歷HTML的節點樹,從每一個anchor元素的href屬性獲得l
|
||||
```Go
|
||||
// visit appends to links each link found in n and returns the result.
|
||||
func visit(links []string, n *html.Node) []string {
|
||||
if n.Type == html.ElementNode && n.Data == "a" {
|
||||
for _, a := range n.Attr {
|
||||
if a.Key == "href" {
|
||||
links = append(links, a.Val)
|
||||
}
|
||||
}
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
links = visit(links, c)
|
||||
}
|
||||
return links
|
||||
if n.Type == html.ElementNode && n.Data == "a" {
|
||||
for _, a := range n.Attr {
|
||||
if a.Key == "href" {
|
||||
links = append(links, a.Val)
|
||||
}
|
||||
}
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
links = visit(links, c)
|
||||
}
|
||||
return links
|
||||
}
|
||||
```
|
||||
|
||||
@@ -109,21 +109,21 @@ http://www.google.com/intl/en/policies/privacy/
|
||||
```Go
|
||||
gopl.io/ch5/outline
|
||||
func main() {
|
||||
doc, err := html.Parse(os.Stdin)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "outline: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
outline(nil, doc)
|
||||
doc, err := html.Parse(os.Stdin)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "outline: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
outline(nil, doc)
|
||||
}
|
||||
func outline(stack []string, n *html.Node) {
|
||||
if n.Type == html.ElementNode {
|
||||
stack = append(stack, n.Data) // push tag
|
||||
fmt.Println(stack)
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
outline(stack, c)
|
||||
}
|
||||
if n.Type == html.ElementNode {
|
||||
stack = append(stack, n.Data) // push tag
|
||||
fmt.Println(stack)
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
outline(stack, c)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -153,10 +153,10 @@ $ ./fetch https://golang.org | ./outline
|
||||
|
||||
大部分編程語言使用固定大小的函數調用棧,常見的大小從64KB到2MB不等。固定大小棧會限製遞歸的深度,當你用遞歸處理大量數據時,需要避免棧溢出;除此之外,還會導致安全性問題。與相反,Go語言使用可變棧,棧的大小按需增加(初始時很小)。這使得我們使用遞歸時不必考慮溢出和安全問題。
|
||||
|
||||
練習**5.1** :脩改findlinks代碼中遍歷n.FirstChild鏈表的部分,將循環調用visit,改成遞歸調用。
|
||||
**練習 5.1:** 脩改findlinks代碼中遍歷n.FirstChild鏈表的部分,將循環調用visit,改成遞歸調用。
|
||||
|
||||
練習**5.2** : 編寫函數,記録在HTML樹中出現的同名元素的次數。
|
||||
**練習 5.2:** 編寫函數,記録在HTML樹中出現的同名元素的次數。
|
||||
|
||||
練習**5.3** : 編寫函數輸出所有text結點的內容。註意不要訪問`<script>`和`<style>`元素,因爲這些元素對瀏覽者是不可見的。
|
||||
**練習 5.3:** 編寫函數輸出所有text結點的內容。註意不要訪問`<script>`和`<style>`元素,因爲這些元素對瀏覽者是不可見的。
|
||||
|
||||
練習**5.4** : 擴展vist函數,使其能夠處理其他類型的結點,如images、scripts和style sheets。
|
||||
**練習 5.4:** 擴展vist函數,使其能夠處理其他類型的結點,如images、scripts和style sheets。
|
||||
|
||||
Reference in New Issue
Block a user