ch5-5: fmt code

pull/1/head
chai2010 2016-01-06 20:30:05 +08:00
parent 423ec9de12
commit 03edd41bdb
1 changed files with 40 additions and 35 deletions

View File

@ -1,15 +1,19 @@
## 5.5. 函數值
在Go中函數被看作第一類值first-class values函數像其他值一樣擁有類型可以被賦值給其他變量傳遞給函數從函數返迴。對函數值function value的調用類似函數調用。例子如下
```Go
func square(n int) int { return n * n }
func negative(n int) int { return -n }
func product(m, n int) int { return m * n }
f := square
fmt.Println(f(3)) // "9"
f = negative
fmt.Println(f(3)) // "-3"
fmt.Println(f(3)) // "-3"
fmt.Printf("%T\n", f) // "func(int) int"
f = product // 編譯錯誤: can't assign f(int, int) int to f(int) int
```
@ -17,7 +21,7 @@
```Go
var f func(int) int
f(3) //此處f的值爲nil,會引起panic錯誤
f(3) // 此處f的值爲nil,會引起panic錯誤
```
函數值可以與nil比較
@ -35,9 +39,10 @@
```Go
func add1(r rune) rune { return r + 1 }
fmt.Println(strings.Map(add1, "HAL-9000")) // "IBM.:111"
fmt.Println(strings.Map(add1, "VMS"))// "WNT"
fmt.Println(strings.Map(add1, "Admix")) // "Benjy"
fmt.Println(strings.Map(add1, "VMS")) // "WNT"
fmt.Println(strings.Map(add1, "Admix")) // "Benjy"
```
5.2節的findLinks函數使用了輔助函數visit,遍歷和操作了HTML頁面的所有結點。使用函數值我們可以將遍歷結點的邏輯和操作結點的邏輯分離使得我們可以複用遍歷的邏輯從而對結點進行不同的操作。
@ -48,48 +53,48 @@ gopl.io/ch5/outline2
// pre和post都是可選的。
// 遍歷孩子結點之前,pre被調用
// 遍歷孩子結點之後post被調用
func forEachNode(n *html.Node, pre, post func(n *html.Node)) {
if pre != nil {
pre(n)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
forEachNode(c, pre, post)
}
if post != nil {
post(n)
}
func forEachNode(n *html.Node, pre, post func(n *html.Node)) {
if pre != nil {
pre(n)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
forEachNode(c, pre, post)
}
if post != nil {
post(n)
}
}
```
該函數接收2個函數作爲參數分别在結點的孩子被訪問前和訪問後調用。這樣的設計給調用者更大的靈活性。舉個例子現在我們有startElemen和endElement兩個函數用於輸出HTML元素的開始標籤和結束標籤< b >。。。< /b>
該函數接收2個函數作爲參數分别在結點的孩子被訪問前和訪問後調用。這樣的設計給調用者更大的靈活性。舉個例子現在我們有startElemen和endElement兩個函數用於輸出HTML元素的開始標籤和結束標籤`<b>...</b>`
```Go
var depth int
func startElement(n *html.Node) {
if n.Type == html.ElementNode {
fmt.Printf("%*s<%s>\n", depth*2, "", n.Data)
depth++
}
var depth int
func startElement(n *html.Node) {
if n.Type == html.ElementNode {
fmt.Printf("%*s<%s>\n", depth*2, "", n.Data)
depth++
}
func endElement(n *html.Node) {
if n.Type == html.ElementNode {
depth--
fmt.Printf("%*s</%s>\n", depth*2, "", n.Data)
}
}
func endElement(n *html.Node) {
if n.Type == html.ElementNode {
depth--
fmt.Printf("%*s</%s>\n", depth*2, "", n.Data)
}
}
```
上面的代碼利用fmt.Printf的一個小技巧控製輸出的縮進。%\*s中的\*會在字符串之前填充一些空格。在例子中,每次輸出會先填充depth\*2數量的空格再輸出""最後再輸出HTML標籤。
上面的代碼利用fmt.Printf的一個小技巧控製輸出的縮進。`%*s`中的`*`會在字符串之前填充一些空格。在例子中,每次輸出會先填充`depth*2`數量的空格,再輸出""最後再輸出HTML標籤。
如果我們像下面這樣調用forEachNode
```Go
forEachNode(doc, startElement, endElement)
forEachNode(doc, startElement, endElement)
```
與之前的outline程序相比我們得到了更加詳細的頁面結構
```Go
```
$ go build gopl.io/ch5/outline2
$ ./outline2 http://gopl.io
<html>
@ -112,16 +117,16 @@ $ ./outline2 http://gopl.io
...
```
練習 **5.7**完善startElement和endElement函數使其成爲通用的HTML輸出器。要求輸出註釋結點文本結點以及每個元素的屬性< a href='...'>)。使用簡略格式輸出沒有孩子結點的元素(卽用< img/> 代替< img>< /img>。編寫測試驗證程序輸出的格式正確。詳見11章
**練習 5.7** 完善startElement和endElement函數使其成爲通用的HTML輸出器。要求輸出註釋結點文本結點以及每個元素的屬性< a href='...'>)。使用簡略格式輸出沒有孩子結點的元素(卽用`<img/>`代替`<img></img>`。編寫測試驗證程序輸出的格式正確。詳見11章
練習 **5.8**脩改pre和post函數使其返迴布爾類型的返迴值。返迴false時中止forEachNoded的遍歷。使用脩改後的代碼編寫ElementByID函數根據用戶輸入的id査找第一個擁有該id元素的HTML元素査找成功後停止遍歷。
**練習 5.8** 脩改pre和post函數使其返迴布爾類型的返迴值。返迴false時中止forEachNoded的遍歷。使用脩改後的代碼編寫ElementByID函數根據用戶輸入的id査找第一個擁有該id元素的HTML元素査找成功後停止遍歷。
```Go
func ElementByID(doc *html.Node, id string) *html.Node
func ElementByID(doc *html.Node, id string) *html.Node
```
練習 **5.9** :編寫函數 expand,將s中的"foo"替換爲f("foo")的返迴值。
**練習 5.9** 編寫函數expand將s中的"foo"替換爲f("foo")的返迴值。
```Go
func expand(s string, f func(string) string) string
```
func expand(s string, f func(string) string) string
```