-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Proposal for Automatic Error Handling in Go #70151
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
Whether the handler returns true or false, |
I'm not quite sure that I follow the descriptions around the examples as the examples themselves don't seem to show the functionality that the text describes. How would you write the following using your proposal? v, err := strconv.ParseInt(str, 10, 0)
if err != nil {
return fmt.Errorf("failed to parse string: %w", err)
} |
This proposal is primarily aimed at addressing the repeated use of if != nil statements. If your code does not contain any duplicate if != nil statements, you may choose not to use this proposal. However, if if != nil appears multiple times, it is recommended to implement this proposal.
|
Go Programming Experience
Intermediate
Other Languages Experience
python,java,c,nodejs,swift,php,go
Related Idea
Has this idea, or one like it, been proposed before?
Yes, the idea of simplifying error handling in Go has been proposed before in various forms, though not necessarily with the same implementation as this proposal.
Does this affect error handling?
Yes, this proposal does affect error handling, but in a positive way. Here are some key points on how it influences error handling in Go:
In summary, the proposal positively affects error handling by making it simpler, more organized, and easier to read, while remaining compatible with Go's existing error handling practices. It encourages cleaner code and enhances the overall developer experience without introducing breaking changes.
Is this about generics?
No, this proposal is not about generics. The focus of the proposal is on simplifying error handling in Go by introducing a context-based error handling mechanism (errctx) that reduces the repetitive if err != nil checks.
Proposal
First of all, I would like to note that I was very reluctant to make this step, but I really think it is simple and will greatly simplify error handling in Go.
Goal: Simplify repetitive if err != nil blocks across the codebase.
Example: Error handling.
We can generalize the inner block of the if statement, but we still have to process the result with an if statement to exit the outer function. This leads to too many lines of code for something that could be a one-liner.
How it will work
Usage Example
Requirements
This approach will greatly reduce the number of lines written for error handling in an application. Developers can implement general error handlers and utilize them succinctly, leading to more maintainable code.
This method is not only helpful in error handling but also aids in reducing the cognitive workload in functions with multiple if, else, or switch statements.
Language Spec Changes
No response
Informal Change
No response
Is this change backward compatible?
Yes, the proposed change for automatic error handling in Go is designed to be backward compatible. Here are a few key points explaining why:
Overall, since the change does not disrupt current functionality or the syntax of the language, it remains backward compatible, allowing developers to integrate it without affecting existing code.
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
No response
Cost Description
No response
Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: