Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derfer案例存在问题 #8

Open
leojjj opened this issue Nov 11, 2021 · 1 comment
Open

derfer案例存在问题 #8

leojjj opened this issue Nov 11, 2021 · 1 comment

Comments

@leojjj
Copy link

leojjj commented Nov 11, 2021


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

改为输出1

@ghost
Copy link

ghost commented Jul 19, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant