go
via PatrickJS/awesome-cursorrules
Idiomatic Go: explicit error handling, interface-based design, context-first concurrency.
What is go?
Enforces Go best practices for error handling, naming conventions, interface design, and concurrency patterns. Use this rule when writing Go code to ensure consistency with idiomatic Go standards and avoid common pitfalls like silent error ignoring and unsafe goroutine patterns.
- Enforce explicit error handling with fmt.Errorf wrapping and errors.Is/As checking
- Apply idiomatic naming conventions: short vars, no stuttering, acronym casing, -er suffix for interfaces
- Require interfaces at call sites, single-method design, and accept-interfaces/return-concrete patterns
- Mandate context.Context as first parameter for blocking functions with proper defer cancel()
- Enforce table-driven testing and interface-based mocking patterns
- Prohibit error ignoring, init() for business logic, global mutable state, and uncontrolled goroutines
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
Go Language Rules
Expert Go developer. Simple, explicit, idiomatic.
Error Handling
- Always handle errors — never assign to _
- fmt.Errorf("context: %w", err) for wrapping
- errors.Is() / errors.As() for checking
- Custom error types for structured errors
Naming
- Short for short-lived vars: i, n, err, ok
- No stuttering: user.UserID → user.ID
- Acronyms: userID, httpClient (not userId, httpClient)
- Interfaces: end in -er (Reader, Writer, Handler)
Interfaces
- Accept interfaces, return concrete types
- Define at call site, not implementation site
- Single-method interfaces preferred
Concurrency
- context.Context first param for blocking functions
- defer cancel() after context creation
- WaitGroup for goroutine groups
- Channels for communication, Mutex for state
Testing
- Table-driven: for _, tc := range testCases { t.Run(tc.name, ...) }
- Interface-based mocking
Forbidden
- No _ to ignore errors
- No init() for business logic
- No global mutable state
- No interface{} where generics work
- No goroutines without termination condition
Related rules
AI pair programming assistant for Go backend development with scalability focus
Build REST APIs with Go's standard library ServeMux (Go 1.22+)
Design patterns and best practices for building agents, tools, and workflows with Google's Agent Development Kit.
Build graphical LLM applications by chaining reusable Elements with typed Ports and Payloads.
HarmonyOS ArkTS component structure, state management, layout, and accessibility patterns.
