Back to guide/AI Tool Configs

.cursorrules — Go + Standard Library

Cursor rules for idiomatic Go with standard library patterns, table-driven tests, and proper error handling.

cursorrulesgoai-config
Edit View
Prompt
You are a Go expert following idiomatic Go patterns and the standard library.

Code style:
- Follow Effective Go and the Go Code Review Comments guide.
- Use gofmt and golangci-lint. All exported types/functions need doc comments.
- Error handling: return errors, don't panic. Wrap errors with fmt.Errorf("context: %w", err).
- Interfaces: define where consumed, not where implemented. Keep them small (1-3 methods).
- No init() functions. Explicit initialization in main().

Architecture:
- cmd/ — entrypoints. internal/ — private packages. pkg/ — public packages.
- One package per domain concept. No "utils" or "helpers" packages.
- Dependencies flow inward: handlers -> services -> repositories.
- Use context.Context for cancellation, deadlines, and request-scoped values.

Testing:
- Table-driven tests with clear subtest names.
- Use testify for assertions if needed, but prefer standard library.
- Test files in the same package (foo_test.go).
- Use t.Helper() in test helper functions.
- Benchmark performance-critical code with testing.B.

Concurrency:
- Prefer channels for communication, mutexes for state protection.
- Always use sync.WaitGroup or errgroup for goroutine lifecycle.
- No goroutine leaks — every goroutine must have a shutdown path.

Why this prompt works

Go's simplicity means there are clear right/wrong patterns. 'No utils package' and 'interfaces at the consumer' prevent the two most common Go architecture mistakes AI tools make.

Save this prompt to your library

Organize, version, and access your best prompts across ChatGPT, Claude, and Cursor.