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

question #29131

Closed
timchenxiaoyu opened this issue Dec 7, 2018 · 1 comment
Closed

question #29131

timchenxiaoyu opened this issue Dec 7, 2018 · 1 comment

Comments

@timchenxiaoyu
Copy link

question


func det()(result int){
 result = 2
 defer func() {
  result = 1
  }()
 return result
}

return 1
BUT

func det()(int){
 result := 2
 defer func() {
  result = 1
  }()
 return result
}

return 2

why???

@ianlancetaylor
Copy link
Contributor

This is not the best place for questions. Please see https://golang.org/wiki/Questions .

The first example has a named result parameter, the second does not. If a named result is changed in a defer statement, that changes the result. If a local variable is changed in a defer statement, that does not affect the result.

@golang golang locked and limited conversation to collaborators Dec 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants