Back to questions
JuniorTheoryCommon
What does the program print?
package main
import (
"errors"
"fmt"
)
func main() {
errNotFound := errors.New("not found")
err := fmt.Errorf("load user: %w", errNotFound)
fmt.Println(err == errNotFound, errors.Is(err, errNotFound))
}false true
- Expecting
truefrom==:fmt.Errorfwith%wreturns a new error that holdserrNotFound, so the two values differ and only the chain links them. - Expecting
errors.Isto compare like==: it walks the chain that%wbuilds, findserrNotFoundinsideerrand returnstrue.
- What would
errors.Isreturn if the code used%vinstead of%w? - Why is a sentinel error usually declared as a package-level
var?