Go icon indicating copy to clipboard operation
Go copied to clipboard

derfer案例存在问题

Open leojjj opened this issue 3 years ago • 1 comments

二 func t2() (a int) { defer func() { a++ }() return 1 } 输出:2

改为输出1

leojjj avatar Nov 11 '21 08:11 leojjj

应该是2吧,因为定义了返回的变量为a,在return之后(传递的是a的引用),defer后跟着的匿名函数执行后 a变为2,所以返回值应该是2。

ghost avatar Jul 19 '22 13:07 ghost