User Tools

Site Tools


go:errors:error_wrapping

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
go:errors:error_wrapping [2026/01/05 23:17] – created phong2018go:errors:error_wrapping [2026/01/05 23:22] (current) phong2018
Line 24: Line 24:
   * Use `%v` if you only want formatting without wrapping.   * Use `%v` if you only want formatting without wrapping.
   * Wrapping enables `errors.Is` and `errors.As` to work through layers.   * Wrapping enables `errors.Is` and `errors.As` to work through layers.
 +
 +===== Error wrapping (%w) vs formatting (%v) =====
 +
 +==== What is it? ====
 +In `fmt.Errorf`, you can use:
 +  * `%w` to **wrap** an error (keeps the original error inside)
 +  * `%v` to **format** an error (string formatting only; does NOT wrap)
 +
 +==== Why it matters ====
 +  * `%w` enables `errors.Is` / `errors.As` to find the root cause through wrapped layers.
 +  * `%v` only prints the error message; `errors.Is` will not match the original error.
 +
 +==== Example code ====
 +<code go>
 +package main
 +
 +import (
 +    "errors"
 +    "fmt"
 +)
 +
 +var ErrNotFound = errors.New("not found")
 +
 +func repo() error {
 +    return ErrNotFound
 +}
 +
 +// Wrap with %w (keeps original error)
 +func usecaseWrap() error {
 +    if err := repo(); err != nil {
 +        return fmt.Errorf("usecase (wrap): %w", err)
 +    }
 +    return nil
 +}
 +
 +// Format only with %v (does NOT wrap)
 +func usecaseFormatOnly() error {
 +    if err := repo(); err != nil {
 +        return fmt.Errorf("usecase (format only): %v", err)
 +    }
 +    return nil
 +}
 +
 +func main() {
 +    errW := usecaseWrap()
 +    errV := usecaseFormatOnly()
 +
 +    fmt.Println("== %w ==")
 +    fmt.Println("Error:", errW)
 +    fmt.Println("errors.Is(errW, ErrNotFound):", errors.Is(errW, ErrNotFound))
 +
 +    fmt.Println("\n== %v ==")
 +    fmt.Println("Error:", errV)
 +    fmt.Println("errors.Is(errV, ErrNotFound):", errors.Is(errV, ErrNotFound))
 +}
 +</code>
 +
 +==== Example output ====
 +<code>
 +== %w ==
 +Error: usecase (wrap): not found
 +errors.Is(errW, ErrNotFound): true
 +
 +== %v ==
 +Error: usecase (format only): not found
 +errors.Is(errV, ErrNotFound): false
 +</code>
 +
 +==== Rule of thumb ====
 +  * Use `%w` when you want callers to detect the original error using `errors.Is/As`.
 +  * Use `%v` when you only want a formatted message and do not need error matching.
 +
 +==== Related pages ====
 +  * [[go:errors:error_wrapping|Error wrapping (%w)]]
 +  * [[go:errors:errors_is_as|errors.Is / errors.As]]
 +  * [[go:errors:sentinel_errors|Sentinel errors]]
  
 ==== Hard words (English) ==== ==== Hard words (English) ====
   * **wrap** /ræp/: bọc   * **wrap** /ræp/: bọc
-  * **preserve** /prɪˈːv/: giữ li+  * **format** /ˈːrmæt/: định dng
   * **root cause** /ruːt kɔːz/: nguyên nhân gốc   * **root cause** /ruːt kɔːz/: nguyên nhân gốc
 +  * **detect** /dɪˈtekt/: phát hiện/nhận biết
 +  * **matching** /ˈmætʃɪŋ/: so khớp 
 +  * **preserve** /prɪˈzɝːv/: giữ lại
   * **layer** /ˈleɪər/: tầng   * **layer** /ˈleɪər/: tầng
  
go/errors/error_wrapping.1767655060.txt.gz · Last modified: by phong2018