error — value, explicit handling. Exception — not in Go (use panic/recover).
error:
1func ReadFile(path string) ([]byte, error) {2 data, err := os.ReadFile(path)3 if err != nil {4 return nil, err // Return error5 }6 return data, nil7}
Exception (not in Go):
Go philosophy:
Custom errors:
1type ValidationError struct {2 Field string3 Message string4}56func (e *ValidationError) Error() string {7 return fmt.Sprintf("%s: %s", e.Field, e.Message)8}