← Go Programming learning path
Go Programming · Module 7 of 10

Error handling

Handle errors explicitly as values.

📖 Key ideas

  1. Functions return error as the last value.
  2. Check: if err != nil { return err }.
  3. Create errors: errors.New("msg") or fmt.Errorf("...: %w", err).
  4. errors.Is and errors.As inspect wrapped errors.
  5. panic is for unrecoverable situations; recover can catch it.
💡 Real-life example: Go errors are receipts handed back with every delivery: always check if something went wrong.
errorBuilt-in interface for errors
fmt.Errorf %wWraps an error
errors.IsChecks for a specific error
panicStops normal execution