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.
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.
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/golang/comm...