proposal: Go 2: conditional return for better error handling #28229
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
Milestone
The issue of simplifying error handling is nothing new and it has come up many times, there has been various proposals on the topic as well, (e.g., #16225, #18721, #21146, #21155, and more comprehensively #21161).
The general complaint that too much Go code contains these lines or a variation of it.
This proposal suggests the addition of conditional return statement as outlined below:
ConditionalReturnStmt = "return" ( Expression ) [ ExpressionList ]
Where the value of Expression is tested for equality with predeclared untyped value
nil
to determine whatever the return expression should result in the termination of the function. Similar to ReturnStmt, it may optionally provide one or more result values instead of the Expression.This would allow writing code like so:
or given:
Would become:
Though, I am not sure how one could use a Conditional Return as is or make it work with a situation like the following:
The text was updated successfully, but these errors were encountered: