### Checking for Older Go Versions with Compile Errors Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Provides example compile errors that indicate an older Go version is being used, suggesting the need for Go 1.13 or newer for go-multierror. ```go /go/src/github.com/hashicorp/go-multierror/multierror.go:112:9: undefined: errors.As /go/src/github.com/hashicorp/go-multierror/multierror.go:117:9: undefined: errors.Is ``` -------------------------------- ### Basic Error Aggregation: go-multierror vs. Standard Library Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Compares how to aggregate errors using go-multierror's Append function versus the standard library's errors.Join. ```go // Before (go-multierror) var result error result = multierror.Append(result, err1) result = multierror.Append(result, err2) return result ``` ```go // After (stdlib) var errs []error if err1 != nil { errs = append(errs, err1) } if err2 != nil { errs = append(errs, err2) } if len(errs) > 0 { return errors.Join(errs...) } return nil ``` -------------------------------- ### Custom Error Formatting with go-multierror Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Demonstrates how go-multierror allows for custom error formatting, which can be achieved in the standard library by wrapping the joined error with a custom type. ```go // go-multierror allows custom formatting // With stdlib, wrap the joined error with a custom type if needed type FormattedError struct { errs []error } func (e *FormattedError) Error() string { // Your custom format here return fmt.Sprintf("multiple errors: %v", e.errs) } func (e *FormattedError) Unwrap() []error { return e.errs } ``` -------------------------------- ### Concurrent Error Collection: go-multierror Group vs. errgroup with Mutex Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Illustrates how to collect all errors concurrently. The standard library's errgroup returns only the first error, while a custom ErrorCollector with a mutex can aggregate all errors. ```go // golang.org/x/sync/errgroup returns only the first error: import "golang.org/x/sync/errgroup" g := new(errgroup.Group) g.Go(func() error { return task1() }) g.Go(func() error { return task2() }) err := g.Wait() // Returns first error only // To collect ALL errors like multierror.Group does, use a mutex: type ErrorCollector struct { mu sync.Mutex errs []error } func (ec *ErrorCollector) Add(err error) { if err != nil { ec.mu.Lock() ec.errs = append(ec.errs, err) ec.mu.Unlock() } } func (ec *ErrorCollector) Err() error { ec.mu.Lock() defer ec.mu.Unlock() if len(ec.errs) == 0 { return nil } return errors.Join(ec.errs...) } ``` -------------------------------- ### Customize Error Formatting Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Specify a custom ErrorFormat function to change how the Error() string is generated for a multierror. ```go var result *multierror.Error // ... accumulate errors here, maybe using Append if result != nil { result.ErrorFormat = func([]error) string { return "errors!" } } ``` -------------------------------- ### Return Error Only If Present Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Use ErrorOrNil to return a nil error if no errors were accumulated, otherwise return the multierror. ```go var result *multierror.Error // ... accumulate errors here // Return the `error` only if errors were added to the multierror, otherwise // return nil since there are no errors. return result.ErrorOrNil() ``` -------------------------------- ### Append Errors to a List Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Use the Append function to build a list of errors. It handles nil or existing error types gracefully. ```go var result error if err := step1(); err != nil { result = multierror.Append(result, err) } if err := step2(); err != nil { result = multierror.Append(result, err) } return result ``` -------------------------------- ### Extract Specific Error with errors.As Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Use the standard errors.As function to check for and extract a specific error type from a multierror. ```go // Assume err is a multierror value err := somefunc() // We want to know if "err" has a "RichErrorType" in it and extract it. var errRich RichErrorType if errors.As(err, &errRich) { // It has it, and now errRich is populated. } ``` -------------------------------- ### Check for Exact Error with errors.Is Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Use the standard errors.Is function to determine if a multierror contains a specific error value. ```go // Assume err is a multierror value err := somefunc() if errors.Is(err, os.ErrNotExist) { // err contains os.ErrNotExist } ``` -------------------------------- ### Access Multierror List via Type Switch Source: https://github.com/hashicorp/go-multierror/blob/main/README.md Use a type switch to check if an error is a *multierror.Error and access its Errors field. ```go if err := something(); err != nil { if merr, ok := err.(*multierror.Error); ok { // Use merr.Errors } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.