码迷,mamicode.com
首页 > 其他好文 > 详细

closures _ golang

时间:2015-03-14 18:28:43      阅读:107      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!