Documentation
¶
Overview ¶
package multierr provides a simple way to collect errors and return them as a single error using errors.Join.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collector ¶
type Collector interface {
// Add appends an error to the collection.
Add(error)
// Err returns all collected errors as a single error, by calling errors.Join.
// If no errors have been added, Err returns nil.
Err() error
}
Collector collects multiple errors and returns them as a single error. Errors can be added by multierr.Collector.Add, and the final combined error is obtained by multierr.Collector.Err.
The resulting error supports errors.Is and errors.As checks for any of the collected errors.
func New ¶
func New() Collector
New creates a new Collector.
This function is not concurrency-safe: if you plan to use the collector from multiple goroutines, consider wrapping calls with a mutex or using your own concurrency control.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/no-yan/multierr"
)
func main() {
m := multierr.New()
reasons := []string{"1st error", "2nd error"}
for _, reason := range reasons {
err := errors.New(reason)
m.Add(err)
}
fmt.Println(m.Err())
}
Output: 1st error 2nd error
Click to show internal directories.
Click to hide internal directories.