-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: introduce try/guard keyword for error handling #39890
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
Comments
What are the semantics of I think the thing that really killed the last |
@carlmjohnson could you link that proposal? Not sure which one you're referring to- I feel it's the one I couldn't find in my research? |
@carlmjohnson removed that syntax as it obscured the flow of the error variable (and |
I'm still not sure I get what adding some kind of try semantic to the language would solve that is not already addressed by |
I think this is exactly right. It doesn't go far enough to satisfy the people who find writing |
I beg do disagree. I don't think you could reduce this much more unless sacrificing other aspects:
The number of error handling proposals, including those of go team members, shows that there is a wider need. |
At the same time, the fact that the proposals keep getting shot down indicates that the need so far has not outweighed the added complexity. |
...absolutely. And since this one doesn‘t even develop traction in terms of comments or votes I‘m simply closing it to save everybody‘s time. Thanks for sharing your thoughts! |
Go has
suffered fromseen a number of Go2 error handling proposals. I'm joining the chorus as I had the feeling that a comparatively less disruptive permutation of the discussion has not come up before. I'd be totally happy if this got closed right away, in that case sorry for wasting everybody's time.Goals of this proposal:
Before going to the details, I have considered:
guard
in proposal: Go 2: block scoped error handling #33161 (comment).try()
Proposal
This is an adapted version of #33161 (comment), I couldn't find an original proposal to this case (/cc @carlmjohnson).
In short, I'm proposing to introduce a
try
keyword similar to Swift'sguard
that will be implemented as an error-checking and error-handling specific alternative to the existingif
statement:try
must me followed by an assignment expression where the last assignment parameter must be an error and can be omittedtry
statement has a handler block, the block will be executed/ the error can be handled similar toif err != nil
and returned or not. This typically requires the err parameter to be named.if
statement, a special scoping rule allows the assigned target variables (right term?) to escape the block (I understand this is similar to swift). This includes the error variable if it is named.As such, this proposal is similar to #33161 (comment), taking the comments regarding control flow from #33161 (comment) into account.
Syntactically, the two following statements are equivalent:
It would look like
Examples
CopyFile
Update:
Obviously this example needs an additional rule for error precedence duringUpdated thedefer
(tbd).defer
Syntax. Usingtry
inside deferred functions follows the same rules as everywhere else.Hex
Some thoughts
As @carlmjohnson said:
guard
because that's a name used by another language.The text was updated successfully, but these errors were encountered: