### Every - Check if All Errors Match Target in Go Source: https://context7.com/uber-go/multierr/llms.txt Compares every error in a given error against a target error using errors.Is. It returns true only if all comparisons return true. Nil errors are handled as vacuously true. ```go package main import ( "errors" "fmt" "io" "go.uber.org/multierr" ) func main() { // All errors are EOF allEOF := multierr.Combine( io.EOF, fmt.Errorf("wrapped: %w", io.EOF), io.EOF, ) fmt.Println(multierr.Every(allEOF, io.EOF)) // Output: true // Not all errors are EOF mixedErrors := multierr.Combine( io.EOF, errors.New("different error"), io.EOF, ) fmt.Println(multierr.Every(mixedErrors, io.EOF)) // Output: false // Single error singleErr := io.EOF fmt.Println(multierr.Every(singleErr, io.EOF)) // Output: true // Nil error returns true (vacuous truth) fmt.Println(multierr.Every(nil, io.EOF)) // Output: true } ``` -------------------------------- ### AppendFunc - Shorthand for Deferred Function Calls in Go Source: https://context7.com/uber-go/multierr/llms.txt Provides a shorthand for AppendInvoke, allowing direct use of function or method values without explicit Invoker interface wrapping. This simplifies deferred calls to functions that return errors. ```go package main import ( "fmt" "go.uber.org/multierr" ) type Worker struct { name string } func (w *Worker) Stop() error { fmt.Printf("Stopping worker: %s\n", w.name) return nil // or return an error if stop fails } func doSomething() (err error) { w := &Worker{name: "processor"} // AppendFunc is a convenient shorthand // multierr will call w.Stop() when function returns defer multierr.AppendFunc(&err, w.Stop) // Do work... fmt.Println("Working...") return nil } func main() { if err := doSomething(); err != nil { fmt.Println("Error:", err) } // Output: // Working... // Stopping worker: processor } ``` -------------------------------- ### Integration with errors.Is and errors.As in Go Source: https://context7.com/uber-go/multierr/llms.txt Demonstrates how multierr integrates with Go's standard library error introspection functions. Combined errors implement the multi-error interface, supporting errors.Is and errors.As for unwrapping and checking error types. ```go package main import ( "errors" "fmt" "io" "os" "go.uber.org/multierr" ) // Custom error type for errors.As type ValidationError struct { Field string Msg string } func (e *ValidationError) Error() string { return fmt.Sprintf("validation error on %s: %s", e.Field, e.Msg) } func main() { // errors.Is works with combined errors err := multierr.Combine( errors.New("first error"), io.EOF, os.ErrNotExist, ) fmt.Println(errors.Is(err, io.EOF)) // Output: true fmt.Println(errors.Is(err, os.ErrNotExist)) // Output: true // errors.As works with combined errors errWithType := multierr.Combine( errors.New("generic error"), &ValidationError{Field: "email", Msg: "invalid format"}, ) var valErr *ValidationError if errors.As(errWithType, &valErr) { fmt.Printf("Found validation error: %s\n", valErr.Field) // Output: Found validation error: email } // Wrapped errors are also matched wrappedErr := multierr.Combine( fmt.Errorf("wrapped EOF: %w", io.EOF), errors.New("other error"), ) fmt.Println(errors.Is(wrappedErr, io.EOF)) // Output: true } ``` -------------------------------- ### Extract Individual Errors in Go Source: https://context7.com/uber-go/multierr/llms.txt Returns a slice of individual errors contained within a multierr value. If the input error is nil, it returns nil. If the error is not a multierr, it returns a slice containing only that error. This is useful for iterating over and processing each distinct error. ```go package main import ( "errors" "fmt" "go.uber.org/multierr" ) func main() { // Build a combined error err := multierr.Combine( nil, // successful request errors.New("call 2 failed"), errors.New("call 3 failed"), ) err = multierr.Append(err, nil) // successful request err = multierr.Append(err, errors.New("call 5 failed")) // Extract individual errors for processing errs := multierr.Errors(err) for i, e := range errs { fmt.Printf("Error %d: %v\n", i+1, e) } // Output: // Error 1: call 2 failed // Error 2: call 3 failed // Error 3: call 5 failed // Returns nil for nil error fmt.Println(multierr.Errors(nil)) // Output: [] // Returns slice with single error for non-multierr single := errors.New("single error") fmt.Println(multierr.Errors(single)) // Output: [single error] } ``` -------------------------------- ### Append Two Errors in Go Source: https://context7.com/uber-go/multierr/llms.txt Appends two errors together, handling nil values gracefully. This function is an optimized version of Combine for the common case of combining exactly two errors. It's useful for incrementally building an error from sequential operations. ```go package main import ( "errors" "fmt" "go.uber.org/multierr" ) func main() { var err error // Append errors one at a time err = multierr.Append(err, errors.New("call 1 failed")) err = multierr.Append(err, errors.New("call 2 failed")) fmt.Println(err) // Output: call 1 failed; call 2 failed // Append handles nil gracefully err = multierr.Append(nil, errors.New("only error")) fmt.Println(err.Error()) // Output: only error err = multierr.Append(errors.New("first"), nil) fmt.Println(err.Error()) // Output: first } ``` -------------------------------- ### AppendInvoke - Deferred Error Capture in Go Source: https://context7.com/uber-go/multierr/llms.txt Appends the result of calling an Invoker into a provided error pointer. This is useful for deferring fallible operations until a function returns, ensuring errors are captured. It requires an Invoker interface implementation. ```go package main import ( "bufio" "fmt" "io" "os" "go.uber.org/multierr" ) // Process a file with proper cleanup error handling func processFile(path string) (err error) { f, err := os.Open(path) if err != nil { return err } // Close will be called when function returns; if it fails, // the error is appended to the return value defer multierr.AppendInvoke(&err, multierr.Close(f)) return processReader(f) } // Process a reader with scanner error handling func processReader(r io.Reader) (err error) { scanner := bufio.NewScanner(r) // scanner.Err will be called when function returns defer multierr.AppendInvoke(&err, multierr.Invoke(scanner.Err)) for scanner.Scan() { line := scanner.Text() fmt.Println(line) } return nil } // Custom Invoker implementation for directory cleanup type RemoveAll string func (r RemoveAll) Invoke() error { return os.RemoveAll(string(r)) } // Example with custom Invoker func workWithTempDir() (err error) { dir, err := os.MkdirTemp("", "example") if err != nil { return err } // Directory cleanup deferred with custom Invoker defer multierr.AppendInvoke(&err, RemoveAll(dir)) // Do work in temp directory... return nil } ``` -------------------------------- ### Combine Multiple Errors in Go Source: https://context7.com/uber-go/multierr/llms.txt Combines zero or more errors into a single error value. It returns nil if all input errors are nil, the single error if only one is non-nil, or a combined error that flattens nested multierr errors. This function is useful for aggregating errors from independent operations. ```go package main import ( "errors" "fmt" "go.uber.org/multierr" ) func main() { // Combine multiple errors from independent operations err := multierr.Combine( errors.New("call 1 failed"), nil, // successful request errors.New("call 3 failed"), nil, // successful request errors.New("call 5 failed"), ) // Single-line format with %v fmt.Printf("%v\n", err) // Output: call 1 failed; call 3 failed; call 5 failed // Multi-line format with %+v fmt.Printf("%+v\n", err) // Output: // the following errors occurred: // - call 1 failed // - call 3 failed // - call 5 failed // Combine returns nil if all errors are nil noErr := multierr.Combine(nil, nil, nil) fmt.Println(noErr == nil) // Output: true // Combine returns the single error if only one is non-nil singleErr := multierr.Combine(nil, errors.New("only error"), nil) fmt.Println(singleErr.Error()) // Output: only error } ``` -------------------------------- ### Append Error into Pointer and Check in Go Source: https://context7.com/uber-go/multierr/llms.txt Appends an error into the destination of an error pointer and returns a boolean indicating whether the appended error was non-nil. This is particularly useful for building errors within loops, allowing you to track which operations failed. ```go package main import ( "errors" "fmt" "go.uber.org/multierr" ) func main() { var err error // AppendInto returns true if error was non-nil if multierr.AppendInto(&err, errors.New("foo")) { fmt.Println("call 1 failed") } if multierr.AppendInto(&err, nil) { fmt.Println("call 2 failed") // This won't print } if multierr.AppendInto(&err, errors.New("baz")) { fmt.Println("call 3 failed") } fmt.Println(err) // Output: // call 1 failed // call 3 failed // foo; baz } // Common pattern: building errors in a loop func processItems(items []string) error { var err error for _, item := range items { if multierr.AppendInto(&err, validateItem(item)) { fmt.Printf("skipping invalid item: %s\n", item) continue } // Process valid item... } return err } func validateItem(item string) error { if item == "" { return errors.New("empty item") } return nil } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.