闭包函数
闭包函数:定义在函数内部,对外部作用域有引用
import "fmt" func main(){ } //go中函数内部定义的函数是不能有名的,需要定义匿名函数:没有名字 func test(a int){ func(){ fmt.Println("我是内置函数") }() // 这个括号是用来调用匿名函数的 }
func test(a int) (func()){ // func()表示返回值部分 b := func(){ fmt.Println(a) fmt.Println("我是闭包函数") } return b }
func test(a func()) func(){ b := func(){ fmt.Println("我先执行") a() fmt.Println("函数执行完了") } return b } func test2(){ fmt.Println("xxx") }
type MyFunc func(a int,b int) func() type MyInt int func test() (MyFunc) { c:= func(a int,b int) func(){ return func() { } } return c } func main() { }