| name | go-backend-coding-guidelines |
| description | Enforce shared coding standards for Go backend projects. Use when writing, refactoring, or reviewing server-side Go code, especially for HTTP handlers, services/use-cases, data-access layers, background workers, and API error/response contracts. |
Go Backend Coding Guidelines
Apply this skill as the default quality layer for backend Go code changes.
Core Priorities
Apply in this order:
- YAGNI: implement only what the current milestone needs.
- KISS: favor obvious code over abstraction-heavy designs.
- DRY: deduplicate proven repetition, not speculative repetition.
- Consistency: prefer existing repo conventions over personal preference.
Mandatory Architecture Rules
- Keep HTTP handlers thin: parse input, call service, map error/response.
- Keep business rules in services/use-cases, not in transport layers.
- Keep data-access details in repository/store packages.
- Keep package responsibilities explicit and narrow.
- Avoid deep cross-package internals imports.
- Avoid hidden global mutable state; pass dependencies explicitly.
Backend Domain Invariants
Never violate these constraints:
- Keep trust boundaries explicit (authn/authz, tenancy, internal-only endpoints).
- Preserve consistency for write operations (validation, persistence, side effects).
- Keep idempotency rules explicit for mutating endpoints/jobs where relevant.
- Keep public API contracts stable unless intentionally versioned.
- Keep internal/private resources inaccessible from public APIs.
- Keep side effects deterministic and observable (logs/metrics/events).
API and Error Conventions
- Return stable JSON error envelopes: machine-readable
error and human-readable message.
- Validate early; fail with specific domain error types (
validation_failed, not_found, conflict, etc.).
- Keep response payloads predictable and typed; avoid polymorphic surprises.
- Keep endpoint semantics deterministic for pagination, filtering, and sorting.
- Keep optional behavior flags/headers documented and consistently applied.
Go Style Baseline
- Prefer stdlib-first solutions.
- Keep interfaces small and consumer-driven.
- Keep constructors explicit (
NewX(...)) and dependency-injected.
- Keep functions short and single-purpose.
- Add comments for package intent and non-obvious logic, not trivial lines.
- Keep concurrency explicit and local (locks/channels scoped tightly).
Package Documentation Rule (doc.go)
For every exported package, add and maintain a doc.go with a consistent structure.
Use the template in references/doc-go-template.md.
Change Workflow
For each non-trivial change:
- Identify the target layer (handler, service, store, domain).
- Define invariants and failure modes before coding.
- Implement the smallest complete slice.
- Add/adjust tests at the same layer as the behavior change.
- Run quality gates from
references/testing-gates.md.
- Keep package docs (
doc.go) aligned with new responsibilities.
Reference Routing
Read only what is needed:
- Use
references/backend-architecture.md for backend boundaries and invariants.
- Use
references/go-style.md for concrete coding and layout conventions.
- Use
references/doc-go-template.md when creating/updating package docs.
- Use
references/testing-gates.md before finishing implementation.
Do not load all references by default.