Standard library / errors
errors.Is()
Reports whether err or an error in its unwrap tree matches target by equality or a custom Is method. Traverses wrapped children depth first. Prefer this to comparing error text. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func Is(err, target error) boolParameters & Attributes
| Name | Type | Description |
|---|---|---|
| err | error | Error to inspect or unwrap. |
| target | error | Error to match (Is) or non-nil destination pointer (As). |
Return Value
bool
Whether a matching error exists.
Working Example
Example Snippetgo
package main
import ( "errors" "fmt")
func main() { base := errors.New("missing") e := fmt.Errorf("read: %w", base) fmt.Println(errors.Is(e, base))}