Error handling

Sentinel errors

A sentinel is a shared error value for a recognizable condition. Use errors.Is to match it through wrapping rather than comparing text. Each separate errors.New invocation produces a distinct error value even for identical text. Example output: 'true false'.

Formal Syntax & Structure

go syntaxgo
var ErrMissing = errors.New("missing")

Working Example

Example Snippetgo
package main
import "fmt"import "errors"
var ErrMissing = errors.New("missing")
func lookup() error { return ErrMissing }func main() {  fmt.Println(errors.Is(lookup(), ErrMissing), errors.Is(errors.New("missing"), ErrMissing))}
Go Reference