learn go return fuction

package main

// 参考文章:
//     https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.9.md

import "fmt"

func main() {
    var f = Adder()
    fmt.Print(f(1), " - ")
    fmt.Print(f(20), " - ")
    fmt.Print(f(300))
}

func Adder() func(int) int {
    var x int
    return func(delta int) int {
        x += delta
        return x
    }
}


//terminal output: 1 - 31 - 321

 

posted on 2015-11-22 18:28  zengjf  阅读(173)  评论(0编辑  收藏  举报

导航