匿名函数
//直接执行
func main() {
func(s string) {
println(s)
}("hello,world!")
}
//复制给变量
func main() {
add:=func(x,y int)int{
return x+y
}
println(add(1,2))
}
//作为参数
func test(f func()) {
f()
}
func main() {
test(func() {
println("hello,world!")
})
}
//作为返回值
func test()func(int,int)int{
return func(x,y int)int{
return x+y
}
}
func maidn() {
add:=test()
println(add(1,2))
}
//普通函数和匿名函数都可作为结构体字段,或经通道传递
func testStruct() {
type calc struct{ // 定义结构体类型
mul func(x,y int)int // 函数类型字段
}
x:=calc{
mul:func(x,y int)int{
return x*y
},
}
println(x.mul(2,3))
}
func testChannel() {
c:=make(chan func(int,int)int,2)
c<-func(x,y int)int{
return x+y
}
println((<-c)(1,2))
}
//不曾使用的匿名函数会被编译器当作错误
func main() {
func(s string) { // 错误:func literal evaluated but not used
println(s)
} // 此处并未调用
}
闭包
闭包(closure)是在其词法上下文中引用了自由变量的函数,或者说是函数和其引用的环境的组合体
func test(x int)func() {
return func() {
println(x)
}
}
func main() {
f:=test(123)
f()
}
123
就这段代码而言,test返回的匿名函数会引用上下文环境变量x。当该函数在main中执行时,它依然可正确读取x的值,这种现象就称作闭包。
闭包是如何实现的?匿名函数被返回后,为何还能读取环境变量值?修改一下代码再看
package main
func test(x int)func() {
println(&x)
return func() {
println(&x,x)
}
}
func main() {
f:=test(0x100)
f()
}
0xc82000a100
0xc82000a100 256
通过输出指针,我们注意到闭包直接引用了原环境变量。分析汇编代码,你会看到返回的不仅仅是匿名函数,还包括所引用的环境变量指针。所以说,闭包是函数和引用环境的组合体更加确切。
小例子
func main() {
for i := 0; i < 3; i++ {
defer func(i int) { fmt.Println(i) }(i) //值拷贝
}
}
2
1
0
func main() {
for i := 0; i < 3; i++ {
defer func() { fmt.Println(i) }()//闭包是引用类型
}
}
3
3
3