Custom Errors and Wrapping
errors.New makes an error and %w wraps one. errors.Is and errors.As inspect chains.
Example
package main
import (
"errors"
"fmt"
)
var ErrNotFound = errors.New("not found")
func load() error { return fmt.Errorf("load user: %w", ErrNotFound) }
func main() {
err := load()
fmt.Println(err)
fmt.Println(errors.Is(err, ErrNotFound))
}Result
load user: not found true
Exercise
Wrap an error.
fmt.Errorf("x: %", err)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.