go_goutils/multierr/doc.go

64 lines
1.2 KiB
Go

/*
Package multierr provides a simple way of handling multiple errors and consolidating them into a single error.
Example:
package main
import (
`r00t2.io/goutils/multierr`
)
func main() {
var err error
var errs []error
errs = make([]error, 0)
for _, i := range someSlice {
go func() {
if err = i.DoSomething(); err != nil {
errs = append(errs, err)
}
}()
}
if errs != nil && len(errs) != 0 {
// err now contains multiple errors presented as a single error interface.
err = multierr.NewErrors(errs...)
}
}
MultiError also has a shorthand, making the above much less verbose:
package main
import (
`r00t2.io/goutils/multierr`
)
func main() {
var err error
var multierror *multierr.MultiError = multierr.NewMultiError(nil)
for _, i := range someSlice {
go func() {
if err = i.DoSomething(); err != nil {
multierror.AddError(err)
}
}()
}
// multierror now contains any/all errors above. If calling in a function, you'll probably want to do:
// if !multierror.IsEmpty() {
// err = multierror
// }
}
In the above, the multierror assignment can still be used as an error.
*/
package multierr