Go Errors

Go error handling patterns. Routes to specific patterns.

majiayu000 Updated 567 repo stars

File contents

Go Error Handling

Route by Need

  • Wrapping errors with context → see wrapping/
  • Defining sentinel errors → see sentinel/
  • Checking error types → see checking/

Quick Check

  • Never ignore errors (_ = fn())
  • Wrap with context at boundaries
  • Use errors.Is/As, not ==

Common Pattern

func ProcessFile(path string) error {
    f, err := os.Open(path)
    if err != nil {
        return fmt.Errorf("open %s: %w", path, err)
    }
    defer f.Close()

    if err := parse(f); err != nil {
        return fmt.Errorf("parse %s: %w", path, err)
    }
    return nil
}

Resources

  • wrapping/ - Add context with fmt.Errorf %w
  • sentinel/ - Define package-level errors
  • checking/ - Type-safe error inspection

majiayu000/claude-skill-registry-data/tree/main/testing/go-errors commit 16a88c1ff7

Frequently asked questions

npx skillmds@latest add majiayu000/go-errors