Video summary

The Best Way to Compare Errors in Go: Using errors.Is & errors.As with Code Examples

Main summary

Key takeaways

Technology

Comparing and Handling Errors in Go

The video tutorial focuses on effectively comparing and handling errors in Go using the standard library functions errors.Is and errors.As, with practical code examples.

Key Concepts and Features Covered

1. Limitations of Simple Error Comparison

  • Direct equality checks (==) fail when sentinel errors are wrapped inside other errors.
  • This necessitates specialized functions to traverse error chains.

2. errors.Is Function

  • Signature: func Is(err, target error) bool
  • Purpose: Checks if an error or any error in its wrapped chain matches a specific sentinel error instance.
  • Works by unwrapping errors recursively and comparing them to the target error.
  • Supports customization by implementing an Is(error) bool method on custom error types.
  • Demonstrated by creating a custom sentinel error and verifying detection even when wrapped.
  • Practical example using fs.ErrNotExist to detect “file does not exist” errors from the os package.

3. errors.As Function

  • Signature: func As(err error, target interface{}) bool
  • Purpose: Finds the first error in the chain that matches the type of the target, and assigns it to the target pointer.
  • Useful for extracting specific error types and accessing their custom fields or methods.
  • Requires the target to be a non-nil pointer to a type implementing error or an interface.
  • Supports customization via an As(interface{}) bool method on custom error types.
  • Illustrated with a custom error struct containing additional fields (e.g., status code) and accessing these fields after type assertion.
  • Also shown with the standard library’s fs.PathError to access fields like Op and Path.

4. Error Wrapping and Unwrapping

  • Both errors.Is and errors.As traverse wrapped error chains transparently, making error handling more robust.

5. Summary and Best Practices

  • Use errors.Is when checking for specific sentinel errors.
  • Use errors.As when you need to extract and work with specific error types and their fields.
  • These functions improve error handling clarity and maintainability in Go programs.

Tutorials and Guides Provided

  • Creating and using custom sentinel errors.
  • Wrapping errors and checking for wrapped sentinel errors with errors.Is.
  • Using errors.As to extract custom error types and access their fields.
  • Handling standard library errors like fs.ErrNotExist and fs.PathError.

Main Speaker / Source

The tutorial is presented by the course instructor of a GoLang tutorial series (tutorial #52), who provides live coding demonstrations and explanations throughout the video.

Rate this summary

Your feedback will help improve summaries.

Improve this summary

Reprocess with a stronger model when the summary feels incomplete or inaccurate.

Pro

Translate summary in another language

Pro

Ask questions to this video

Chat for follow-up questions, clarifications, and source-backed answers.

Coming soon

Share this summary

Original video