How to Use the errors.Join Function in Go 1.20+

errors.Join combines multiple Go errors into a single error value for unified handling and inspection.

Use errors.Join to combine multiple error values into a single error that can be checked with errors.Is or errors.As.

import "errors"

err1 := fmt.Errorf("failed to read file")
err2 := fmt.Errorf("failed to write file")
combined := errors.Join(err1, err2)

if combined != nil {
    // Handle combined error
}