zoukankan      html  css  js  c++  java
  • [tgpl]go匿名函数

    [tgpl]go匿名函数

    0. 定义

    匿名函数顾名思义是没有名字的函数,
    Named functions can be declared only at the package level, but we can use a function literal to denote a function value within any expression.

    1. 最简单的例子Map

    strings.Map(func(r rune) rune { return r + 1 }, "HAL-9000")
    

    strings 包里面的Map函数,第一个参数是mapping,一个函数对象。直接用匿名函数操作很完美。

    2. squares

    gopl.io/ch5/squares
         // squares returns a function that returns
         // the next square number each time it is called.
         func squares() func() int {
             var x int
             return func() int {
                 x++
                 return x * x
    } }
         func main() {
             f := squares()
             fmt.Println(f()) // "1"
             fmt.Println(f()) // "4"
             fmt.Println(f()) // "9"
             fmt.Println(f()) // "16"
    }
    

    很有意思,suqares函数返回一个匿名函数,里面有个local变量,匿名函数能访问到那个local变量,这点就很神奇了。
    原文是
    More importantly, functions defined in this way have access to the entire lexical environment, so the inner function can refer to variables from the enclosing function

    如果你定义一个g,里面的变量x也是独立的,跟f的x不是一个。

    	g := squares()
    	fmt.Println(g())
    	fmt.Println(g())
    	fmt.Println(g())
    	fmt.Println(g())
    

    3. defer 里面调用匿名函数

     func bigSlowOperation() {
             defer trace("bigSlowOperation")() // don't forget the extra parentheses
             // ...lots of work...
             time.Sleep(10 * time.Second) // simulate slow operation by sleeping
    }
         func trace(msg string) func() {
             start := time.Now()
             log.Printf("enter %s", msg)
             return func() { log.Printf("exit %s (%s)", msg, time.Since(start)) }
    }
    
    

    bigSlowOperation里面加了个defer trace,而trace返回了一个匿名函数,so 匿名函数里面的local变量对该匿名函数是可见的,所以time.Since能访问到start,也就算出了整个操作的时间。

    from

    引用tgpl ch5。

  • 相关阅读:
    html5+css3兼容问题小结
    webkit滚动条样式设置
    rem彻底研究
    四月、五月开发总结
    php oci8 小试
    oracle创建dblink
    php 页面参数过多时自动拼接get参数的函数
    Oracle Database 11g Express Edition 使用小结(windows)
    php sortable 动态排序
    php动态生成一个xml文件供swf调用
  • 原文地址:https://www.cnblogs.com/gqdw/p/12808682.html
Copyright © 2011-2022 走看看