// Chapter 33 example: %w keeps the original error inside, %v keeps only its text. // Run with: go run exercises/33/02-wrap.go package main import ( "errors" "fmt" "os" ) func readWithW(name string) error { _, err := os.ReadFile(name) if err != nil { return fmt.Errorf("reading settings: %w", err) } return nil } func readWithV(name string) error { _, err := os.ReadFile(name) if err != nil { return fmt.Errorf("reading settings: %v", err) } return nil } func main() { errW := readWithW("no-such-file.txt") errV := readWithV("no-such-file.txt") fmt.Println(errW) fmt.Println(errV) fmt.Printf("readWithW still holds ErrNotExist: %v\n", errors.Is(errW, os.ErrNotExist)) fmt.Printf("readWithV still holds ErrNotExist: %v\n", errors.Is(errV, os.ErrNotExist)) fmt.Println("Unwrapped once:", errors.Unwrap(errW)) }