Skip to content

defer_return.md "The defer function must be a function literal declared inside the caller." is not correct #1

Description

@tv42

https://github.com/seebs/notes/blob/master/defer_return.md

It is possible for a deferred function call to modify the return value of its caller in go. For this to happen, two things must be the case: [...] The defer function must be a function literal declared inside the caller.

This is not correct.

func inc(p *int) { *p++ }

func foo() (n int) {
    defer inc(&n)
    return 42
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions