W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
原文鏈接:https://gopl-zh.github.io/ch5/ch5-05.html
在Go中,函數(shù)被看作第一類值(first-class values):函數(shù)像其他值一樣,擁有類型,可以被賦值給其他變量,傳遞給函數(shù),從函數(shù)返回。對函數(shù)值(function value)的調(diào)用類似函數(shù)調(diào)用。例子如下:
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.Printf("%T\n", f) // "func(int) int"
f = product // compile error: can't assign func(int, int) int to func(int) int
函數(shù)類型的零值是nil。調(diào)用值為nil的函數(shù)值會引起panic錯誤:
var f func(int) int
f(3) // 此處f的值為nil, 會引起panic錯誤
函數(shù)值可以與nil比較:
var f func(int) int
if f != nil {
f(3)
}
但是函數(shù)值之間是不可比較的,也不能用函數(shù)值作為map的key。
函數(shù)值使得我們不僅僅可以通過數(shù)據(jù)來參數(shù)化函數(shù),亦可通過行為。標(biāo)準(zhǔn)庫中包含許多這樣的例子。下面的代碼展示了如何使用這個技巧。strings.Map對字符串中的每個字符調(diào)用add1函數(shù),并將每個add1函數(shù)的返回值組成一個新的字符串返回給調(diào)用者。
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"
5.2節(jié)的findLinks函數(shù)使用了輔助函數(shù)visit,遍歷和操作了HTML頁面的所有結(jié)點。使用函數(shù)值,我們可以將遍歷結(jié)點的邏輯和操作結(jié)點的邏輯分離,使得我們可以復(fù)用遍歷的邏輯,從而對結(jié)點進行不同的操作。
gopl.io/ch5/outline2
// forEachNode針對每個結(jié)點x,都會調(diào)用pre(x)和post(x)。
// pre和post都是可選的。
// 遍歷孩子結(jié)點之前,pre被調(diào)用
// 遍歷孩子結(jié)點之后,post被調(diào)用
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)
}
}
該函數(shù)接收2個函數(shù)作為參數(shù),分別在結(jié)點的孩子被訪問前和訪問后調(diào)用。這樣的設(shè)計給調(diào)用者更大的靈活性。舉個例子,現(xiàn)在我們有startElement和endElement兩個函數(shù)用于輸出HTML元素的開始標(biāo)簽和結(jié)束標(biāo)簽<b>...</b>
:
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)
}
}
上面的代碼利用fmt.Printf的一個小技巧控制輸出的縮進。%*s
中的*
會在字符串之前填充一些空格。在例子中,每次輸出會先填充depth*2
數(shù)量的空格,再輸出"",最后再輸出HTML標(biāo)簽。
如果我們像下面這樣調(diào)用forEachNode:
forEachNode(doc, startElement, endElement)
與之前的outline程序相比,我們得到了更加詳細的頁面結(jié)構(gòu):
$ go build gopl.io/ch5/outline2
$ ./outline2 http://gopl.io
<html>
<head>
<meta>
</meta>
<title>
</title>
<style>
</style>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<a>
<img>
</img>
...
練習(xí) 5.7: 完善startElement和endElement函數(shù),使其成為通用的HTML輸出器。要求:輸出注釋結(jié)點,文本結(jié)點以及每個元素的屬性(< a href='...'>)。使用簡略格式輸出沒有孩子結(jié)點的元素(即用<img/>
代替<img></img>
)。編寫測試,驗證程序輸出的格式正確。(詳見11章)
練習(xí) 5.8: 修改pre和post函數(shù),使其返回布爾類型的返回值。返回false時,中止forEachNoded的遍歷。使用修改后的代碼編寫ElementByID函數(shù),根據(jù)用戶輸入的id查找第一個擁有該id元素的HTML元素,查找成功后,停止遍歷。
func ElementByID(doc *html.Node, id string) *html.Node
練習(xí) 5.9: 編寫函數(shù)expand,將s中的"foo"替換為f("foo")的返回值。
func expand(s string, f func(string) string) string
![]() | ![]() |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: