errors.Is checks for a specific error value, while errors.As extracts a custom error type to access its fields.
Use errors.Is to check if an error matches a specific sentinel value, and errors.As to extract a custom error type from a wrapped error chain.
if errors.Is(err, os.ErrNotExist) {
// Handle specific error
}
if var errTarget *MyCustomError; errors.As(err, &errTarget) {
// Access custom fields: errTarget.Code
}
Use errors.Is like a security guard checking an ID to see if an error is exactly the one you expect. Use errors.As like a translator that pulls out a specific type of error from a complex message so you can read its details. You use Is for simple checks and As when you need to access extra information stored in a custom error.