Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
What is the best way to call a function that returns a value and error, so that the value is placed into function scope but the error isn't?
Post Body

This is easy to accomplish when you're calling a function that only returns an error. You can do:

if err := whatever(); err != nil {
    return err
}

In the outer scope, err remains undefined, which is what I want. But when calling a function that returns a value as well as an error, this is the best I've been able to come up with:

var exe string
{
    var err error
    exe, err = os.Executable()
    if err != nil {
        return err
    }
}

You might ask, who cares if err winds up in the outer scope? You can just redeclare it with := over and over, and nothing bad happens. I completely agree, and that's what I do most of the time. I just don't like it, so I'm interested in whether anyone can come up with a more terse or elegant way to keep err out of the outer scope.

Author
Account Strength
100%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
214,870
Link Karma
2,148
Comment Karma
210,265
Profile updated: 1 day ago
Posts updated: 6 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago