Sentinel error pattern

The sentinel error pattern defines a unique error variable to identify specific failure conditions for precise error handling.

The sentinel error pattern in Go uses a pre-declared error variable to represent a specific, known error condition that callers can check against using errors.Is or ==.

// Define the sentinel error once in the package
var ErrTooLong = errors.New("bufio.Scanner: token too long")

// Return it from a function when the condition occurs
func Scan() error {
    if len(token) > MaxScanTokenSize {
        return ErrTooLong
    }
    return nil
}

// Check for it in the caller
if err != nil && errors.Is(err, ErrTooLong) {
    // Handle the specific case
}