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。

  • 相关阅读:
    MOSS发生“未能转换部分或所有标识引用”错误的处理
    Excel嵌入网页(二) 转载
    GridView的PagerTemplate分页
    深入探讨:如何理解.Net的三层架构(转)
    IIS报Service Unavailable错的解决方案
    浅谈C#托管程序中的资源释放问题(转)
    C#程序节省内存
    网页中嵌入Excel控件(补充)
    解决ajax的status=0问题
    翻开我的博客之旅
  • 原文地址:https://www.cnblogs.com/gqdw/p/12808682.html
Copyright © 2011-2022 走看看