• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

LICENSEH A D24-Jan-20141 KiB

README.mdH A D24-Jan-20141.2 KiB

multierror.goH A D24-Jan-2014917

multierror_test.goH A D24-Jan-20141,014

README.md

1# multierror #
2
3multierror is a simple Go package for combining multiple `error`s.
4This is handy if you are concurrently running operations within
5a function that returns only a single `error`.
6
7## API ##
8
9multierror exposes two types.
10
11`multierror.Errors` is a `[]error` with a receiver method `Err()`,
12which returns a `multierror.MultiError` instance or `nil`.  You use
13this type to collect your errors by appending to it.
14
15`multierror.MultiError` implements the `error` interface.  Its
16`Errors` field contains the `multierror.Errors` you originally
17constructed.
18
19## Example ##
20
21```go
22package main
23
24import (
25	"fmt"
26	"github.com/joeshaw/multierror"
27)
28
29func main() {
30	// Collect multiple errors together in multierror.Errors
31	var e1 multierror.Errors
32	e1 = append(e1, fmt.Errorf("Error 1"))
33	e1 = append(e1, fmt.Errorf("Error 2"))
34
35	// Get a multierror.MultiError from it
36	err := e1.Err()
37
38	// Output: "2 errors: Error 1; Error 2"
39	fmt.Println(err)
40
41	// Iterate over the individual errors
42	merr := err.(*multierror.MultiError)
43	for _, err := range merr.Errors {
44		fmt.Println(err) // Output: "Error 1" and "Error 2"
45	}
46
47	// If multierror.Errors contains no errors, its Err() returns nil
48	var e2 multierror.Errors
49	err = e2.Err()
50
51	// Output: "<nil>"
52	fmt.Println(err)
53}
54```
55