مرکزی مواد پر جائیں
eLearner.app
ماڈیول 6 · سبق 5 از 5کورس میں 30/50~14 min
ماڈیول اسباق (5/5)

غلطی کا انٹرفیس

error in Go isn't a keyword: it's a built-in interface with a single method.

Go
type error interface {
    Error() string
}

Anything that has Error() string is an error. Combined with the convention of returning (T, error) from functions, it's the foundation of error handling in Go.

Sentinel errors

Go
import "errors"

var ErrNotFound = errors.New("non trovato")

func find(key string) (string, error) {
    // ...
    return "", ErrNotFound
}

A sentinel is an exported error value that the caller can compare against to recognize a specific error case.

Custom errors (with data)

To carry context (e.g. the missing key) you define a type:

Go
type ErrNotFound struct {
    Key string
}

func (e *ErrNotFound) Error() string {
    return "non trovato: " + e.Key
}

func find(key string) (string, error) {
    return "", &ErrNotFound{Key: key}
}

errors.Is and errors.As (Go 1.13+)

Comparing with == works only for "pure" sentinels. To be robust against wrapped errors, use the errors package:

Go
import "errors"

if errors.Is(err, ErrNotFound) {
    // err IS ErrNotFound (even if wrapped)
}

var nfe *ErrNotFound
if errors.As(err, &nfe) {
    // err IS (or wraps) an *ErrNotFound; nfe points to the concrete value
    fmt.Println("chiave mancante:", nfe.Key)
}
  • errors.Is(err, target): true if target is somewhere in the wrapping chain.
  • errors.As(err, &target): assigns to target the first error in the chain of its type.

Wrapping with %w

Go
if err := find("k"); err != nil {
    return fmt.Errorf("operazione X: %w", err)
}

%w (NOT %s or %v) creates a new error that wraps the original, keeping the chain navigable by errors.Is/As.

Handling pattern

Go
v, err := op()
if err != nil {
    return fmt.Errorf("op fallita per %q: %w", id, err)
}

Idioms:

  • Check err != nil immediately, no "early happy path".
  • Add context at each level with fmt.Errorf("... : %w", err).
  • "Upstream" (near main / handler), discriminate with errors.Is/As.

Try it

ورزش#go.m6.l5.e1
کوششیں: 0لوڈ ہو رہا ہے…

Implement Error() string on *ErrNotFound so it returns 'non trovato: <Key>'.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

`fmt.Println(err)` automatically calls the Error() method on your struct.

3 کوششوں کے بعد حل دستیاب ہے۔

ورزش#go.m6.l5.e2
کوششیں: 0لوڈ ہو رہا ہے…

Use errors.Is to check whether err equals the sentinel ErrVuoto.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

`errors.Is(err, target)` is robust even with wrapping chains.

3 کوششوں کے بعد حل دستیاب ہے۔

Quiz#go.m6.l5.e3
تیار

What is the exact signature of the method to implement error?

Go
func (e *MyErr) ??? {}
جواب کے اختیارات

Recap

  • error = interface { Error() string }, built-in.
  • Sentinels with errors.New("...") for known cases without context.
  • Custom error types with fields to carry context.
  • errors.Is(err, target): comparison robust to wrapping.
  • errors.As(err, &target): extraction of the concrete type.
  • fmt.Errorf("...: %w", err): wrapping that preserves the chain.
  • Never %v / %s to wrap: ALWAYS use %w.