• 周六. 7 月 27th, 2024

    Go语言教程: 5.5 函数值

    root

    4 月 24, 2021 #Go语言教程, #函数值

    在Go中,函数被看作第一类值(first-class values):函数像其他值一样,拥有类型,可以被赋值给其他变量,传递给函数,从函数返回。对函数值(function value)的调用类似函数调用。例子如下:

        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
    

    函数类型的零值是nil。调用值为nil的函数值会引起panic错误:

        var f func(int) int
        f(3) // 此处f的值为nil, 会引起panic错误
    

    函数值可以与nil比较:

        var f func(int) int
        if f != nil {
            f(3)
        }
    

    但是函数值之间是不可比较的,也不能用函数值作为map的key。

    函数值使得我们不仅仅可以通过数据来参数化函数,亦可通过行为。标准库中包含许多这样的例子。下面的代码展示了如何使用这个技巧。strings.Map对字符串中的每个字符调用add1函数,并将每个add1函数的返回值组成一个新的字符串返回给调用者。

        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节的findLinks函数使用了辅助函数visit,遍历和操作了HTML页面的所有结点。使用函数值,我们可以将遍历结点的逻辑和操作结点的逻辑分离,使得我们可以复用遍历的逻辑,从而对结点进行不同的操作。

    gopl.io/ch5/outline2

    // forEachNode针对每个结点x,都会调用pre(x)和post(x)。
    // 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)
        }
    }
    

    该函数接收2个函数作为参数,分别在结点的孩子被访问前和访问后调用。这样的设计给调用者更大的灵活性。举个例子,现在我们有startElemen和endElement两个函数用于输出HTML元素的开始标签和结束标签<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数量的空格,再输出””,最后再输出HTML标签。

    如果我们像下面这样调用forEachNode:

    forEachNode(doc, startElement, endElement)
    

    与之前的outline程序相比,我们得到了更加详细的页面结构:

    $ 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>
    ...
    

    练习 5.7: 完善startElement和endElement函数,使其成为通用的HTML输出器。要求:输出注释结点,文本结点以及每个元素的属性(< a href=’…’>)。使用简略格式输出没有孩子结点的元素(即用<img/>代替<img></img>)。编写测试,验证程序输出的格式正确。(详见11章)

    练习 5.8: 修改pre和post函数,使其返回布尔类型的返回值。返回false时,中止forEachNoded的遍历。使用修改后的代码编写ElementByID函数,根据用户输入的id查找第一个拥有该id元素的HTML元素,查找成功后,停止遍历。

    func ElementByID(doc *html.Node, id string) *html.Node
    

    练习 5.9: 编写函数expand,将s中的”foo”替换为f(“foo”)的返回值。

    func expand(s string, f func(string) string) string

    root