Grokking Advanced Go
0% completed
Sentinel and Typed Errors
An order store cannot find order 1001, and it has to tell its caller. The three functions below report that same failure, and each one gives the caller something different.
var ErrOrderNotFound = errors.New("order not found") // A value that callers can test for. func findA(id int64) error { return ErrOrderNotFound } // A type with fields that callers can read. func findB(id int64) error { return &NotFoundError{OrderID: id} } // Only a message: callers learn that the call failed. func findC(id int64) error { return fmt.Errorf("order %d not found", id) }
All three return an
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%