且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

learn go anonymous function

更新时间:2022-08-12 21:11:49

package main

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

import "fmt"

func main() {
    f()
}

func f() {
    for i := 0; i < 4; i++ {
        g := func(i int) { fmt.Printf("%d ", i) }
        g(i)
        fmt.Printf(" -g is of type %T and has value %v\n", g, g)
    }
}