Golang Mastery (Senior → Principal)
Operate
- Start by confirming: goal, scope, constraints (Go version, platform, deps), non-functional requirements (latency/throughput/SLO), and “done”.
- Prefer small, end-to-end changes with tests; keep diffs easy to review (unless the user asks otherwise).
- Stdlib-first and “boring” solutions by default; add dependencies only when constraints justify them.
- Explain tradeoffs briefly; keep output actionable (commands, file paths, checklists).
The goal is not “it works locally”, but “it survives production”: clear boundaries, observable behavior, secure defaults, and low ops cost.
Default Go Standards
- Keep packages cohesive; avoid “util” grab-bags; name packages by responsibility, not type.
- Export only what must be used externally; keep APIs minimal and stable.
- Use
context.Context for cancellation/timeouts at boundaries; pass ctx as first arg.
- Return errors, do not log-and-return the same error; wrap with
%w for context.
- Avoid global state; prefer dependency injection via constructors and interfaces.
- Avoid premature interfaces; introduce interfaces at the consumer boundary.
- Concurrency: avoid shared mutable state; prefer channels for coordination, mutexes for invariants; always handle cancellation and shutdown.
“Bad vs Good” (common production pitfalls)
// ❌ BAD: log-and-return (double logging), no useful context
if err != nil {
slog.Error("create user failed", "err", err)
return err
}
// ✅ GOOD: log once at the boundary; wrap error with context
if err != nil {
return fmt.Errorf("create user: %w", err)
}
Workflow (Feature / Refactor / Bug)
- Reproduce or specify behavior (tests first if feasible).
- Sketch design: public API, package boundaries, data shapes, failure modes, concurrency model.
- Implement smallest slice end-to-end.
- Add/adjust tests and benchmarks (when performance-sensitive).
- Validate: formatting, vet, tests, race (when relevant).
- Review for production readiness: config, logging, metrics, timeouts, retries, shutdown.
Validation Commands
- Run
gofmt -w (go list -f '{{.Dir}}' ./...) (PowerShell: gofmt -w @(go list -f '{{.Dir}}' ./...)) and ensure no diffs remain.
- Run
go test ./....
- Run
go test -race ./... for concurrent code or servers (where supported).
- Run
go test -run TestName -count=1 ./path to avoid cached results when debugging.
- Run
go test -bench . -benchmem ./... for hotspots.
- If available, run
golangci-lint run (or golangci-lint run --fast-only during local iteration).
- If available, run
govulncheck ./... (or govulncheck -test ./...) before release.
If you want PowerShell entrypoints:
- sanity:
scripts/go_sanity.ps1
- security/vuln:
scripts/go_security.ps1
Project Structure & Dependencies
See references/project-structure.md for pragmatic layouts by project size, dependency direction guidance, and package naming heuristics.
Tooling (Up-to-date defaults)
See references/tooling.md for a safe baseline linter set, handling false positives, and configuration patterns.
Advanced Topics (Production)
Use these when the project demands “senior+” rigor (many sections include ❌/✅ patterns):
- Arsitektur & boundaries: references/architecture.md
- Principal playbook (casebook + decision tree): references/principal-playbook.md
- Coding standards (review-required): references/coding-standards.md
- Anti-patterns & bug traps: references/anti-patterns.md
- Concurrency (race/leak/backpressure): references/concurrency.md
- Errors & taxonomy: references/errors.md
- HTTP API & status codes: references/http-api.md
- gRPC (contracts, deadlines, streaming, retries): references/grpc.md
- Frameworks (Gin/Fiber/Beego): references/frameworks.md
- Database & queries: references/database.md
- Migrations and rollbacks (deploy-safe schema change): references/migrations-and-rollbacks.md
- Query engineering (Postgres/SQLC): references/query-engineering.md
- Auth (sessions/JWT/OAuth2): references/auth.md
- Multi-tenant systems (isolation, quotas, blast radius): references/multi-tenant-systems.md
- Outbound HTTP & SSRF hardening: references/outbound-http.md
- Idempotency & outbox: references/idempotency-outbox.md
- Security: references/security.md
- Reliability (timeouts, retries, backpressure): references/reliability.md
- Observability (OTel, correlation): references/observability.md
- Incident runbooks (triage, rollback, blast-radius control): references/incident-runbooks.md
- Log/trace correlation: references/log-correlation.md
- OTel bootstrap: references/otel-bootstrap.md
- Performance (pprof/trace/allocs): references/performance.md
- Testing advanced: references/testing-advanced.md
- Distributed systems: references/distributed-systems.md
- Examples (real-world skeleton): references/examples.md
- Patterns (DI/options/circuit-breaker): references/patterns.md
- Snippets (copy/paste-safe): references/snippets.md
Service/HTTP Defaults
- Set timeouts: server
ReadHeaderTimeout, client Timeout/transport timeouts; avoid unbounded requests.
- Always implement graceful shutdown: close listeners, stop accepting, drain, cancel contexts, waitgroups.
- Prefer structured logs; include request IDs; avoid logging secrets/PII.
- Add health endpoints (
/healthz, /readyz) and basic metrics hooks.
Docker & Release Defaults
See references/docker.md for secure, reproducible Docker patterns (multi-stage builds, non-root, no latest, health checks).
Data & Persistence Defaults
- Validate inputs at the boundary; normalize early.
- Make error taxonomy explicit (not found, conflict, invalid, unavailable).
- Prefer migrations and explicit schemas; keep transactional boundaries clear.
- Keep domain logic independent of transport (HTTP/GRPC/CLI) where feasible.
Debugging & Performance
- Use
pprof and benchmarks for performance claims; measure before/after.
- Watch allocations; prefer
strings.Builder, bytes.Buffer, preallocation when proven necessary.
- Avoid reflection-heavy paths in hot loops unless unavoidable.
Security Checklist (Minimum)
- Validate and bound untrusted input (size, depth, time).
- Use
crypto/rand for security tokens; never roll your own crypto.
- Store secrets in env/secret managers; never log secrets.
- Prefer least privilege and explicit allowlists for network/file operations.
Library Selection (Practical)
See references/libraries.md for a conservative shortlist (stdlib-first) plus config/env decoding examples.
Code Review Checklist
See references/checklists.md and references/code-review.md for PR checklists (API, concurrency, testing, ops).
Snippets
See references/snippets.md for copy/paste-safe patterns (graceful shutdown, context plumbing, worker pools).
Scripts
scripts/go_sanity.ps1 - runs gofmt, go test, go vet, optional -race, and golangci-lint if installed.
scripts/go_security.ps1 - runs govulncheck + quick hardening checks (and optional gosec/staticcheck if installed).
scripts/init_project.py - bootstraps a clean Go project skeleton (optional).
1---2name: golang-mastery-skill3description: Principal/Senior-level Go (Golang) playbook for architecture, idiomatic code, concurrency, testing, performance, reliability, security, observability, and production readiness. Use when: designing services/APIs/CLIs, doing large refactors, running code reviews, debugging races/leaks, tuning performance, hardening security, and shipping to production.4---56# Golang Mastery (Senior → Principal)78## Operate910- Start by confirming: goal, scope, constraints (Go version, platform, deps), non-functional requirements (latency/throughput/SLO), and “done”.11- Prefer small, end-to-end changes with tests; keep diffs easy to review (unless the user asks otherwise).12- Stdlib-first and “boring” solutions by default; add dependencies only when constraints justify them.13- Explain tradeoffs briefly; keep output actionable (commands, file paths, checklists).1415> The goal is not “it works locally”, but “it survives production”: clear boundaries, observable behavior, secure defaults, and low ops cost.1617## Default Go Standards1819- Keep packages cohesive; avoid “util” grab-bags; name packages by responsibility, not type.20- Export only what must be used externally; keep APIs minimal and stable.21- Use `context.Context` for cancellation/timeouts at boundaries; pass `ctx` as first arg.22- Return errors, do not log-and-return the same error; wrap with `%w` for context.23- Avoid global state; prefer dependency injection via constructors and interfaces.24- Avoid premature interfaces; introduce interfaces at the consumer boundary.25- Concurrency: avoid shared mutable state; prefer channels for coordination, mutexes for invariants; always handle cancellation and shutdown.2627## “Bad vs Good” (common production pitfalls)2829```go30// ❌ BAD: log-and-return (double logging), no useful context31if err != nil {32 slog.Error("create user failed", "err", err)33 return err34}3536// ✅ GOOD: log once at the boundary; wrap error with context37if err != nil {38 return fmt.Errorf("create user: %w", err)39}40```4142## Workflow (Feature / Refactor / Bug)43441. Reproduce or specify behavior (tests first if feasible).452. Sketch design: public API, package boundaries, data shapes, failure modes, concurrency model.463. Implement smallest slice end-to-end.474. Add/adjust tests and benchmarks (when performance-sensitive).485. Validate: formatting, vet, tests, race (when relevant).496. Review for production readiness: config, logging, metrics, timeouts, retries, shutdown.5051## Validation Commands5253- Run `gofmt -w (go list -f '{{.Dir}}' ./...)` (PowerShell: `gofmt -w @(go list -f '{{.Dir}}' ./...)`) and ensure no diffs remain.54- Run `go test ./...`.55- Run `go test -race ./...` for concurrent code or servers (where supported).56- Run `go test -run TestName -count=1 ./path` to avoid cached results when debugging.57- Run `go test -bench . -benchmem ./...` for hotspots.58- If available, run `golangci-lint run` (or `golangci-lint run --fast-only` during local iteration).59- If available, run `govulncheck ./...` (or `govulncheck -test ./...`) before release.6061If you want PowerShell entrypoints:62- sanity: `scripts/go_sanity.ps1`63- security/vuln: `scripts/go_security.ps1`6465## Project Structure & Dependencies6667See [references/project-structure.md](references/project-structure.md) for pragmatic layouts by project size, dependency direction guidance, and package naming heuristics.6869## Tooling (Up-to-date defaults)7071See [references/tooling.md](references/tooling.md) for a safe baseline linter set, handling false positives, and configuration patterns.7273## Advanced Topics (Production)7475Use these when the project demands “senior+” rigor (many sections include ❌/✅ patterns):7677- Arsitektur & boundaries: [references/architecture.md](references/architecture.md)78- Principal playbook (casebook + decision tree): [references/principal-playbook.md](references/principal-playbook.md)79- Coding standards (review-required): [references/coding-standards.md](references/coding-standards.md)80- Anti-patterns & bug traps: [references/anti-patterns.md](references/anti-patterns.md)81- Concurrency (race/leak/backpressure): [references/concurrency.md](references/concurrency.md)82- Errors & taxonomy: [references/errors.md](references/errors.md)83- HTTP API & status codes: [references/http-api.md](references/http-api.md)84- gRPC (contracts, deadlines, streaming, retries): [references/grpc.md](references/grpc.md)85- Frameworks (Gin/Fiber/Beego): [references/frameworks.md](references/frameworks.md)86- Database & queries: [references/database.md](references/database.md)87- Migrations and rollbacks (deploy-safe schema change): [references/migrations-and-rollbacks.md](references/migrations-and-rollbacks.md)88- Query engineering (Postgres/SQLC): [references/query-engineering.md](references/query-engineering.md)89- Auth (sessions/JWT/OAuth2): [references/auth.md](references/auth.md)90- Multi-tenant systems (isolation, quotas, blast radius): [references/multi-tenant-systems.md](references/multi-tenant-systems.md)91- Outbound HTTP & SSRF hardening: [references/outbound-http.md](references/outbound-http.md)92- Idempotency & outbox: [references/idempotency-outbox.md](references/idempotency-outbox.md)93- Security: [references/security.md](references/security.md)94- Reliability (timeouts, retries, backpressure): [references/reliability.md](references/reliability.md)95- Observability (OTel, correlation): [references/observability.md](references/observability.md)96- Incident runbooks (triage, rollback, blast-radius control): [references/incident-runbooks.md](references/incident-runbooks.md)97- Log/trace correlation: [references/log-correlation.md](references/log-correlation.md)98- OTel bootstrap: [references/otel-bootstrap.md](references/otel-bootstrap.md)99- Performance (pprof/trace/allocs): [references/performance.md](references/performance.md)100- Testing advanced: [references/testing-advanced.md](references/testing-advanced.md)101- Distributed systems: [references/distributed-systems.md](references/distributed-systems.md)102- Examples (real-world skeleton): [references/examples.md](references/examples.md)103- Patterns (DI/options/circuit-breaker): [references/patterns.md](references/patterns.md)104- Snippets (copy/paste-safe): [references/snippets.md](references/snippets.md)105106## Service/HTTP Defaults107108- Set timeouts: server `ReadHeaderTimeout`, client `Timeout`/transport timeouts; avoid unbounded requests.109- Always implement graceful shutdown: close listeners, stop accepting, drain, cancel contexts, waitgroups.110- Prefer structured logs; include request IDs; avoid logging secrets/PII.111- Add health endpoints (`/healthz`, `/readyz`) and basic metrics hooks.112113## Docker & Release Defaults114115See [references/docker.md](references/docker.md) for secure, reproducible Docker patterns (multi-stage builds, non-root, no `latest`, health checks).116117## Data & Persistence Defaults118119- Validate inputs at the boundary; normalize early.120- Make error taxonomy explicit (not found, conflict, invalid, unavailable).121- Prefer migrations and explicit schemas; keep transactional boundaries clear.122- Keep domain logic independent of transport (HTTP/GRPC/CLI) where feasible.123124## Debugging & Performance125126- Use `pprof` and benchmarks for performance claims; measure before/after.127- Watch allocations; prefer `strings.Builder`, `bytes.Buffer`, preallocation when proven necessary.128- Avoid reflection-heavy paths in hot loops unless unavoidable.129130## Security Checklist (Minimum)131132- Validate and bound untrusted input (size, depth, time).133- Use `crypto/rand` for security tokens; never roll your own crypto.134- Store secrets in env/secret managers; never log secrets.135- Prefer least privilege and explicit allowlists for network/file operations.136137## Library Selection (Practical)138139See [references/libraries.md](references/libraries.md) for a conservative shortlist (stdlib-first) plus config/env decoding examples.140141## Code Review Checklist142143See [references/checklists.md](references/checklists.md) and [references/code-review.md](references/code-review.md) for PR checklists (API, concurrency, testing, ops).144145## Snippets146147See [references/snippets.md](references/snippets.md) for copy/paste-safe patterns (graceful shutdown, context plumbing, worker pools).148149## Scripts150151- `scripts/go_sanity.ps1` - runs `gofmt`, `go test`, `go vet`, optional `-race`, and `golangci-lint` if installed.152- `scripts/go_security.ps1` - runs `govulncheck` + quick hardening checks (and optional `gosec`/`staticcheck` if installed).153- `scripts/init_project.py` - bootstraps a clean Go project skeleton (optional).