01 / 04

What is the difference between errors.Is and errors.As?

errors.Is checks if any error in the chain matches a specific sentinel error by value. errors.As checks if any error in the chain can be assigned to a specific type and extracts it.

errors.Is and errors.As examples
When to use each
  1. 1

    errors.Is: comparing against sentinel errors like sql.ErrNoRows, io.EOF, context.DeadlineExceeded

  2. 2

    errors.As: extracting typed errors to access additional fields (HTTP status code, DB error code)

  3. 3

    Both unwrap the error chain — wrapping with %w is essential for them to work

  4. 4

    errors.Is uses == comparison by default; implement Is(error) bool on custom types for custom matching

  5. 5

    errors.As uses type assignability — it finds the first error in chain assignable to the target type