func test() func() string {
s := "a"
return func() string {
return s+"a"
}
}
func test() func() string {
s := "a"
return func() string {
s += "a"
return s
}
}
为啥只有下面这个才是闭包
```go
package main
import (
"fmt"
)
func test() func() string {
s := "a"
return func() string { return s + "a" }
}
func test2() func() string {
s := "a"
return func() string {
s += "a"
return s
}
}
func main() {
tmp := test()
fmt.Println(tmp())
fmt.Println(tmp())
fmt.Println(tmp())
tmp2 := test2()
fmt.Println(tmp2())
fmt.Println(tmp2())
fmt.Println(tmp2())
}
```
输出:
```bash
aa
aa
aa
aa
aaa
aaaa
```
同意三楼
#4
更多评论
```go
func test() func() string {
s := "a"
return func() string {
return s+"a"
}
}
```
应该是所引用的变量s没有发生变化
可以参考这里:https://www.ibm.com/developerworks/cn/linux/l-cn-closure/index.html#note_3
#1