-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: Go2: error handling shortening #62378
Comments
@gopherbot add error-handling |
@gopherbot add v2 |
person := if PersonByID(id); return Hard to understand what this statement does. Saves a couple of keystrokes, but takes all the readability away with it in exchange. Not a fair deal IMO. |
May be this is what you are looking for? https://go.dev/blog/errors-are-values |
Your "before change" code doesn't look right, because it has Does this proposal have any way to handle |
better inplement return return
|
Anyone who proposes a change to error handling should demonstrate the proposal's benefits based on code like this: person, err := PersonByID(id)
if err != nil {
return fmt.Errorf("could not find person by ID %d: %w", id, err)
} not on code like this: person, err := PersonByID(id)
if err != nil {
return err
} as the latter is a bad practice that we certainly do not aim to make easier. |
I must admit, I don't particularly like my proposal myself. My primary hope was actually to maybe get people to start thinking/talking about how we could utilize existing keywords to solve this issue in a meaningful way, rather than adding a new one. |
@chad-bekmezian-snap – How would you define the issue? Considering that in Go, errors are values just like any other, why would we want to create a special syntax to deal with them? |
The issues of actually HANDLING errors/verbosity that were described in the initial draft for error handling that introduced handle/check as documented here: https://go.googlesource.com/proposal/+/master/design/go2draft.md |
We've had several variants of this sort of proposal in the past, which can be found by looking through the issues with the error-handling label. Also, the emoji voting is not in favor. Therefore, this is a likely decline. Leaving open for three weeks for final comments. |
No further comments. |
Would you consider yourself a novice, intermediate, or experienced Go programmer?
intermediate
What other languages do you have experience with?
Java, C#, Javascript, PHP, Python, C++.
Would this change make Go easier or harder to learn, and why?
Only slightly harder as it is largely just syntactic sugar and is fairly easy to read and recognize.
Has this idea, or one like it, been proposed before?
There have been many error handling proposals, so I'm sure there have been similar ones, but none quite like this one I believe.
Many of the other proposals either require the addition of a keyword/identifier in Go (which has backwards compatibility issues),
attempt to add subtle syntax changes (such as "?") to change the flow or behavior of a program, or behave very differently than
typical Go behavior. This proposal attempts to avoid all of those issues.
Who does this proposal help, and why?
Every Go user. It helps cut down on ugly bloated code, and makes for a clear and concise line of error handling.
What is the proposed change?
I propose that the compiler generate the commonly used
err := Func(); err != nil
Please describe as precisely as possible the change to the language.
Compilers need to be changed to recognize an
if
following an assignment operator and generate the same compiled code as itwould in the case of
if err := Func(); err != nil
What would change in the language spec?
This behavior would be documented under if statements
Please also describe the change informally, as in a class teaching Go.
Currently if one would like to handle an error in the case of a method or function that returns multiple values, a developer is
generally required to write five lines of code: one for variable assignment, on for an if statement checking if the error is nil, one for
the opening curly brace, one for the body of the if statement, one for the closing curly brace. This proposal would, in many cases,
mean those five lines are collapsed into one.
Is this change backward compatible?
I believe so.
Show example code before and after the change.
Before change
After change
What is the cost of this proposal? (Every language change has a cost).
Work will have to be done on the compiler. I'm not familiar with the process, but I'm should it would not be particularly easy.
How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
All of them.
What is the compile time cost?
Should be negligible.
What is the run time cost?
No greater than a classic
if err != nil
check is today.How would the language spec change?
Added example and explanation under if statements.
Orthogonality: how does this change interact or overlap with existing features?
Totally orthogonal.
Is the goal of this change a performance improvement?
No
Does this affect error handling?
Yes
Does not introduce any new keywords and reads very similarly to what we have today.
Is this about generics?
No
and other generics proposals?
The text was updated successfully, but these errors were encountered: