defer+返回函数值的语句会立即执行的疑问

wddark · · 1212 次点击
需要理清楚 ```golang defer trace("calculate")() defer trace("calculate") ``` 这两个的区别
#3
更多评论
```go func test(i int) func() func() { fmt.Println("test 1:", i) return func() func() { fmt.Println("test 2:", i) return func() { fmt.Println("test 3:", i) } } } func main() { fmt.Println("a") defer func(i int) { fmt.Println("defer:", i) }(1) defer test(22)()() fmt.Println("b") } ``` 应该是最后一个括号
#1
拷了代码自己跑了遍,结果却不一样: ``` 1 2 2018/02/12 11:48:01 enter calculate 2018/02/12 11:48:01 exit calculate (42.225µs) ```
#2