Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joeshaw/multierror
Go package for encapsulating multiple errors
https://github.com/joeshaw/multierror
golang multierror
Last synced: 13 days ago
JSON representation
Go package for encapsulating multiple errors
- Host: GitHub
- URL: https://github.com/joeshaw/multierror
- Owner: joeshaw
- License: mit
- Created: 2014-01-24T15:18:03.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-01-24T17:43:57.000Z (almost 11 years ago)
- Last Synced: 2024-06-18T17:07:11.541Z (7 months ago)
- Topics: golang, multierror
- Language: Go
- Size: 95.7 KB
- Stars: 27
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# multierror #
multierror is a simple Go package for combining multiple `error`s.
This is handy if you are concurrently running operations within
a function that returns only a single `error`.## API ##
multierror exposes two types.
`multierror.Errors` is a `[]error` with a receiver method `Err()`,
which returns a `multierror.MultiError` instance or `nil`. You use
this type to collect your errors by appending to it.`multierror.MultiError` implements the `error` interface. Its
`Errors` field contains the `multierror.Errors` you originally
constructed.## Example ##
```go
package mainimport (
"fmt"
"github.com/joeshaw/multierror"
)func main() {
// Collect multiple errors together in multierror.Errors
var e1 multierror.Errors
e1 = append(e1, fmt.Errorf("Error 1"))
e1 = append(e1, fmt.Errorf("Error 2"))// Get a multierror.MultiError from it
err := e1.Err()// Output: "2 errors: Error 1; Error 2"
fmt.Println(err)// Iterate over the individual errors
merr := err.(*multierror.MultiError)
for _, err := range merr.Errors {
fmt.Println(err) // Output: "Error 1" and "Error 2"
}// If multierror.Errors contains no errors, its Err() returns nil
var e2 multierror.Errors
err = e2.Err()// Output: ""
fmt.Println(err)
}
```