proposal: Go 2: error handling: set error as return value by default from function #39972
Labels
error-handling
Language & library change proposals that are about error handling.
FrozenDueToAge
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
Milestone
Set Error as return value by default from function
Hi all.
I proposal all functions can be return error by default. In the place where error handling is required, then call the corresponding function (handling() or ifError() or checkError() or etc.), here let it be someNameFunctionErrorHandling().
Do not beat strongly for a long name, it is for educate ;)
This simplifies error handling, do cleaner code and allows you to remember to pass the error up the stack.
See examples for understanding.
Overview
so then
main()
|
˅
func() // if need error processing - "Any Error"
| ^
˅ |
func() // default forwarding error
| ^
˅ |
func() // getting an error - "Any Error"
Examples
Example 1
Old
New (at first glance nothing has changed)
Example 2
Old
New (we are able to immediately process the first error encountered)
Example 3
Old
New (we can forget to forward the error, but not here)
Thank you.
The text was updated successfully, but these errors were encountered: