Best practices for Go Error Handling Patterns
.cursorrules in your project root# Go Error Handling Patterns
## Core Principles
### 1. Errors are Values
* Treat errors as first-class citizens in your code.
* Use errors to handle exceptional cases, not for control flow.
### 2. Explicit Error Handling
* Always check for errors explicitly.
* Do not ignore errors or assume they will not occur.
### 3. Error Types
* Use the `error` type to represent errors.
* Define custom error types when necessary.
## Code Style Guidelines
### Naming
* Use descriptive and concise names for error variables and functions.
* Prefix error variables with `err` to distinguish them from other variables.
### Typing
* Use the `error` type to declare error variables.
* Use type assertions or type switches to handle different error types.
## Best Practices
### 1. Handle Errors Immediately
* Handle errors as soon as they occur.
* Do not propagate errors up the call stack unnecessarily.
### 2. Use Error Wrapping
* Use error wrapping to provide context for errors.
* Use the `%w` directive to wrap errors.
### 3. Log Errors
* Log errors to provide visibility into error occurrences.
* Use a logging library to handle log levels and formatting.
### 4. Test Error Handling
* Test error handling code paths thoroughly.
* Use table-driven tests to cover multiple error scenarios.
## Common Pitfalls to Avoid
### 1. Ignoring Errors
* Never ignore errors or assume they will not occur.
* Always check for errors explicitly.
### 2. Overusing Panics
* Avoid using panics for error handling.
* Use errors to handle exceptional cases, not panics.
### 3. Insufficient Error Context
* Provide sufficient context for errors.
* Use error wrapping and logging to provide context.
### 4. Inconsistent Error Handling
* Handle errors consistently throughout the codebase.
* Establish a standard error handling approach.
## Example Use Cases
### Error Handling Example
```go
func exampleFunc() error {
// Call a function that may return an error
err := anotherFunc()
if err!= nil {
// Handle the error
return fmt.Errorf("exampleFunc: %w", err)
}
// Continue execution
return nil
}
```
### Error Wrapping Example
```go
func exampleFunc() error {
// Call a function that may return an error
err := anotherFunc()
if err!= nil {
// Wrap the error with context
return fmt.Errorf("exampleFunc: failed to call anotherFunc: %w", err)
}
// Continue execution
return nil
}
```
### Logging Example
```go
func exampleFunc() error {
// Call a function that may return an error
err := anotherFunc()
if err!= nil {
// Log the error
log.Errorf("exampleFunc: failed to call anotherFunc: %v", err)
// Handle the error
return err
}
// Continue execution
return nil
}
```Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install Go Error Handling Patterns.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.