golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.94k stars 17.66k forks source link

proposal: Go 2: syntactic sugar for return fmt.Errorf #56159

Closed hherman1 closed 2 years ago

hherman1 commented 2 years ago

Author background

Java, Python, Haskell, C

Related proposals

Proposal

Add a new keyword, e.g try to be used as follows:

val, err := doSomething()
try err: ret1, ret2, fmt.Errorf("doing something: %w", err)
...

In this case, if err == nil then try will return the values on the right of the colon. If err == nil, the routine proceeds. The try keyword would require it's argument to have type error.

Most of my error handling has the form:

if err != nil {
    return fmt.Errorf("context: %w", err)
}

It requires a bit of typing and visual space. It's not too bad, but it is a bit of a nuisance. The aim of this proposal is to change Go programming in precisely this one case, and nowhere else. If you have any sort of complexity in your error handling, you would continue to use the rest of the language to manage your errors. However, in this specific case you can save a bit of typing and a bit of vertical space in your function.

I believe these are the main complaints from those who are bothered by error handling, and they would be satisfied with this proposal.

A new keyword would be added, try, which would be used in the form try [error]: [values...]. [error] would be filled by an expression evaluating to type error. [values...] can be filled by anything you could write after return. If [error] evaluates to a non-nil error, [values...] would be returned.

In other words, this:

val, err := doSomething()
try err: ret1, ret2, fmt.Errorf("doing something: %w", err)
...

Is exactly equal to:

val, err := doSomething()
if err != nil {
    return ret1, ret2, fmt.Errorf("doing something: %w", err)
}
...

I can return to this question and add details if what I wrote above is at all compelling or original.

Use the try keyword as shorthand for simple error checking. You try an error, and if it is not nil, your function will return everything you passed to try.

Costs

hherman1 commented 2 years ago

I hope I'm not wasting your time! I've read many of the error handling proposals over the years, and this has been bouncing around in my head for a while. I don't think I've seen something exactly like it, yet. Hopefully I'm not wrong.

ianlancetaylor commented 2 years ago

See #40432 for a meta-issue. This seems similar to #32611.

hherman1 commented 2 years ago

Oh, ya that's nearly identical to what I'm proposing here. I guess what I've proposed incorporates critique 1 from that proposal. However, if reducing boilerplate is inadequate for this kind of language change (which seems fair to me), then I don't think my proposal is meaningfully different.

I'm going to leave the proposal open so you might confirm that ^ this is true, but I'm happy to close this.

Thanks for the quick response

seankhliao commented 2 years ago

Duplicate of #32611