gopl-zh.github.com/ch5/ch5-05.md

133 lines
4.4 KiB
Markdown
Raw Permalink Normal View History

2016-02-15 03:06:34 +00:00
## 5.5. 函数值
2016-01-06 12:30:05 +00:00
2016-02-15 03:06:34 +00:00
在Go中函数被看作第一类值first-class values函数像其他值一样拥有类型可以被赋值给其他变量传递给函数从函数返回。对函数值function value的调用类似函数调用。例子如下
2015-12-09 07:45:11 +00:00
2016-01-06 11:33:44 +00:00
```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 }
2016-01-06 12:30:05 +00:00
2016-01-06 11:33:44 +00:00
f := square
fmt.Println(f(3)) // "9"
2016-01-06 12:30:05 +00:00
2016-01-06 11:33:44 +00:00
f = negative
2016-01-06 12:30:05 +00:00
fmt.Println(f(3)) // "-3"
2016-01-06 11:33:44 +00:00
fmt.Printf("%T\n", f) // "func(int) int"
2016-01-06 12:30:05 +00:00
2016-01-07 07:02:42 +00:00
f = product // compile error: can't assign func(int, int) int to func(int) int
2016-01-06 11:33:44 +00:00
```
2016-02-15 03:06:34 +00:00
函数类型的零值是nil。调用值为nil的函数值会引起panic错误
2016-01-06 11:33:44 +00:00
```Go
var f func(int) int
2016-02-15 03:06:34 +00:00
f(3) // 此处f的值为nil, 会引起panic错误
2016-01-06 11:33:44 +00:00
```
2016-02-15 03:06:34 +00:00
函数值可以与nil比较
2016-01-06 11:33:44 +00:00
```Go
var f func(int) int
if f != nil {
f(3)
}
```
2016-02-15 03:06:34 +00:00
但是函数值之间是不可比较的也不能用函数值作为map的key。
2016-01-06 11:33:44 +00:00
2016-02-15 03:06:34 +00:00
函数值使得我们不仅仅可以通过数据来参数化函数亦可通过行为。标准库中包含许多这样的例子。下面的代码展示了如何使用这个技巧。strings.Map对字符串中的每个字符调用add1函数并将每个add1函数的返回值组成一个新的字符串返回给调用者。
2016-01-06 11:33:44 +00:00
```Go
func add1(r rune) rune { return r + 1 }
2016-01-06 12:30:05 +00:00
2016-01-06 11:33:44 +00:00
fmt.Println(strings.Map(add1, "HAL-9000")) // "IBM.:111"
2016-01-06 12:30:05 +00:00
fmt.Println(strings.Map(add1, "VMS")) // "WNT"
fmt.Println(strings.Map(add1, "Admix")) // "Benjy"
2016-01-06 11:33:44 +00:00
```
2017-08-24 14:27:42 +00:00
5.2节的findLinks函数使用了辅助函数visit遍历和操作了HTML页面的所有结点。使用函数值我们可以将遍历结点的逻辑和操作结点的逻辑分离使得我们可以复用遍历的逻辑从而对结点进行不同的操作。
2016-01-06 11:33:44 +00:00
2016-01-21 01:58:28 +00:00
<u><i>gopl.io/ch5/outline2</i></u>
2016-01-06 11:33:44 +00:00
```Go
2018-05-27 20:51:15 +00:00
// forEachNode针对每个结点x都会调用pre(x)和post(x)。
2016-02-15 03:06:34 +00:00
// pre和post都是可选的。
2018-05-27 20:51:15 +00:00
// 遍历孩子结点之前pre被调用
2016-02-15 03:06:34 +00:00
// 遍历孩子结点之后post被调用
2016-01-06 12:30:05 +00:00
func forEachNode(n *html.Node, pre, post func(n *html.Node)) {
if pre != nil {
pre(n)
2016-01-06 11:33:44 +00:00
}
2016-01-06 12:30:05 +00:00
for c := n.FirstChild; c != nil; c = c.NextSibling {
forEachNode(c, pre, post)
}
if post != nil {
post(n)
}
}
2016-01-06 11:33:44 +00:00
```
2021-04-26 00:04:43 +00:00
该函数接收2个函数作为参数分别在结点的孩子被访问前和访问后调用。这样的设计给调用者更大的灵活性。举个例子现在我们有startElement和endElement两个函数用于输出HTML元素的开始标签和结束标签`<b>...</b>`
2016-01-06 11:33:44 +00:00
```Go
2016-01-06 12:30:05 +00:00
var depth int
func startElement(n *html.Node) {
if n.Type == html.ElementNode {
fmt.Printf("%*s<%s>\n", depth*2, "", n.Data)
depth++
2016-01-06 11:33:44 +00:00
}
2016-01-06 12:30:05 +00:00
}
func endElement(n *html.Node) {
if n.Type == html.ElementNode {
depth--
fmt.Printf("%*s</%s>\n", depth*2, "", n.Data)
2016-01-06 11:33:44 +00:00
}
2016-01-06 12:30:05 +00:00
}
2016-01-06 11:33:44 +00:00
```
2017-08-24 14:27:42 +00:00
上面的代码利用fmt.Printf的一个小技巧控制输出的缩进。`%*s`中的`*`会在字符串之前填充一些空格。在例子中,每次输出会先填充`depth*2`数量的空格,再输出""最后再输出HTML标签。
2016-01-06 11:33:44 +00:00
2016-02-15 03:06:34 +00:00
如果我们像下面这样调用forEachNode
2016-01-06 11:33:44 +00:00
```Go
2016-01-06 12:30:05 +00:00
forEachNode(doc, startElement, endElement)
2016-01-06 11:33:44 +00:00
```
2016-02-15 03:06:34 +00:00
与之前的outline程序相比我们得到了更加详细的页面结构
2016-01-06 11:33:44 +00:00
2016-01-06 12:30:05 +00:00
```
2016-01-06 11:33:44 +00:00
$ 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>
...
```
2016-02-15 03:06:34 +00:00
**练习 5.7** 完善startElement和endElement函数使其成为通用的HTML输出器。要求输出注释结点文本结点以及每个元素的属性< a href='...'>)。使用简略格式输出没有孩子结点的元素(即用`<img/>`代替`<img></img>`。编写测试验证程序输出的格式正确。详见11章
2016-01-06 11:33:44 +00:00
2016-02-15 03:06:34 +00:00
**练习 5.8** 修改pre和post函数使其返回布尔类型的返回值。返回false时中止forEachNoded的遍历。使用修改后的代码编写ElementByID函数根据用户输入的id查找第一个拥有该id元素的HTML元素查找成功后停止遍历。
2016-01-06 11:33:44 +00:00
```Go
2016-01-06 12:30:05 +00:00
func ElementByID(doc *html.Node, id string) *html.Node
2016-01-06 11:33:44 +00:00
```
2016-01-06 11:36:02 +00:00
2016-02-15 03:06:34 +00:00
**练习 5.9** 编写函数expand将s中的"foo"替换为f("foo")的返回值。
2016-01-06 11:33:44 +00:00
2016-01-06 11:36:53 +00:00
```Go
2016-01-06 12:30:05 +00:00
func expand(s string, f func(string) string) string
```