proposal: Go 2: exclamation assignment error handling #35644
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
Proposes an exclamation identifier ! similar to the blank identifier for assignment. A non zero value assigned to ! causes a run-time error. For example:
Related to the try proposal this proposal aims to simplify error boilerplate. Unlike the try proposal it can be used for any type. The value v assigned to ! effectively calls:
I think this behaviour is similar to how type assertions panic on invalid non handled cases.
Maps can get similar behaviour with
value, ! := myMap[key]
which would panic on missing key.As a larger example Go by Example: Reading Files could be written as:
This removes the check function and calls to check. The panic stack trace would be simplified as the call to check is removed. I could see this being used for example code and simple scripts where panics are already used for error handling.
The text was updated successfully, but these errors were encountered: