Go recently accepted a proposal called “add support for wrapping multiple errors”.
This proposal optimizes error handling and is related to a new feature in Go 1.13 for error handling: Error Wrapping. After introducing Error Wrapping, Go at the same time forerrors
The package adds 3 utility functions, namelyUnwrap
,Is
andAs
.
For the “add support for wrapping multiple errors” proposal, as the name implies, one error can wrap multiple errors.
Unwrap() []error
The developers who made the proposal stated that reusingUnwrap
Avoids ambiguity with the existing Unwrap method, fromUnwrap
Returning a 0-length list means that the error doesn’t wrap anything.The caller must not modify theUnwrap
the returned list,Unwrap
The returned list must not contain anynil
mistake.
he is righterrors.Is
anderrors.As
The function has been updated to implement multiple errorsUnwrap
operate.
errors.Join
function provides a simple implementation of multierr:
// Join returns an error that wraps the given errors.
// Any nil error values are discarded.
// The error formats as the text of the given errors, separated by newlines.
// Join returns nil if errs contains no non-nil values.
func Join(errs ...error) error
The proposal is currently accepted, and the authors say it will be available in Go 1.20:
See https://github.com/golang/go/issues/53435 for details.
#support #wrapping #multiple #errors #News Fast Delivery