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 forerrorsThe package adds 3 utility functions, namelyUnwrap,IsandAs.

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 reusingUnwrapAvoids ambiguity with the existing Unwrap method, fromUnwrapReturning a 0-length list means that the error doesn’t wrap anything.The caller must not modify theUnwrapthe returned list,UnwrapThe returned list must not contain anynilmistake.

he is righterrors.Isanderrors.AsThe function has been updated to implement multiple errorsUnwrapoperate.

errors.Joinfunction 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

Leave a Comment

Your email address will not be published. Required fields are marked *