proposal: spec: reduce error handling boilerplate using catch statement #71498
Labels
error-handling
Language & library change proposals that are about error handling.
LanguageChange
Suggested changes to the Go language
LanguageChangeReview
Discussed by language change review committee
Proposal
Milestone
Proposal Details
Proposal
Reduce boilerplate of handling errors by introducing a new catch statement, which works similar to
switch
andselect
, given the impliciterror
value is non-nil.Multiple return values
Given a function has multiple return values, the catch statement would look like this:
The
default
caseThe
default
case is effectively syntatic sugar for:No
default
provided in catch blockOmitting the
default
case in the catch block, should result in a compilation error for being non-exhaustive.Rationale
Adding a new symbol like
?
or!
, although inspired by other languages, feels out of place in Go code, which is why I believe adding a new keyword instead is a more reasonable approach.Many of the proposals from this meta issue only handle the
err != nil
case, leaving it up to the developer to find a solution when trying to assert on the type of the error.The new
catch
statement feels more Go-like by borrowing well-established syntax fromswitch
andselect
using thecase
keyword to assert on the error type using a boolean expression.Possible considerations for
go fmt
Consider to align error handlers of case statements similar to how map key-value pairs are formatted:
The text was updated successfully, but these errors were encountered: