首页 > 其他 > 详细

closures _ golang

时间:2015-03-14 18:27:21      阅读:233      评论:0      收藏:0      [点我收藏+]

Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it

package main

import (
    "fmt"
)

func intSeq() func() int {
    i := 0
    return func() int {
        i += 1
        return i
    }
}

func main() {

    nextInt := intSeq()

    fmt.Println(nextInt())
    fmt.Println(nextInt())
    fmt.Println(nextInt())

    nextInts := intSeq()
    fmt.Println(nextInts())
}
1
2
3
1

总结 :

  1 : 匿名函数内部的值不是暴露在外面的....

closures _ golang

原文:http://www.cnblogs.com/jackkiexu/p/4337890.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!