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

x/blog: #41541

Closed
sumitgupta153 opened this issue Sep 21, 2020 · 1 comment
Closed

x/blog: #41541

sumitgupta153 opened this issue Sep 21, 2020 · 1 comment
Milestone

Comments

@sumitgupta153
Copy link

@sumitgupta153 sumitgupta153 commented Sep 21, 2020

What version of Go are you using (go version)?

$ go version
I am using go playground and want to point to a blog where example does not seem correct.
https://blog.golang.org/defer-panic-and-recover

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
NA

What did you do?

I am just going through go documentation to understand defer, panic, recover. The example does not seem correct.

What did you expect to see?

Deferred functions may read and assign to the returning function's named return values.

In this example, a deferred function increments the return value i after the surrounding function returns. Thus, this function returns 2:

func c() (i int) {
defer func() { i++ }()
return i
}

What did you see instead?

Deferred functions may read and assign to the returning function's named return values.

In this example, a deferred function increments the return value i after the surrounding function returns. Thus, this function returns 2:

func c() (i int) {
defer func() { i++ }()
return 1
}

@gopherbot gopherbot added this to the Unreleased milestone Sep 21, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Sep 21, 2020

Try it yourself and see. With return 1 the function c returns 2, as the text says. If you change it to return i, then c returns 1.

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.