Back to Rules
Go

Go Microservices Best Practices: Clean Architecture, Observability, Performance & Scalable Backend Development

Claude Directory November 29, 2025
0 copies 1 downloads

Master idiomatic Go development for microservices with Clean Architecture, OpenTelemetry observability, robust testing, security essentials, and performance tuning. Build maintainable, high-performance services ready for production.

Rule Content
- **Core Principles for Go Microservices Development**
  - Focus on writing clean, efficient Go code that's easy to maintain and scale.
  - Adopt Clean Architecture to separate concerns: layer your app with presentation (handlers), business logic (services/use cases), data layer (repositories), and core domain models.
  - Embrace domain-driven design for complex domains and use interface-based development with dependency injection for loose coupling.
  - Favor small interfaces and composition to boost testability and adaptability.

- **Recommended Project Organization**
  - **cmd/**: House main application binaries and entry points.
  - **internal/**: Contain private app logic hidden from external use.
  - **pkg/**: Store reusable utilities and libraries.
  - **api/**: Define API handlers for REST/gRPC and protocol specs.
  - **configs/**: Manage config files and loaders.
  - **test/**: Group testing helpers, mocks, and integration suites.
  - Organize by feature for better cohesion when it enhances readability.

- **Essential Coding Standards**
  - Craft concise functions each handling one clear task.
  - Propagate errors explicitly with context via `fmt.Errorf("detail: %w", err)` for better debugging.
  - Eliminate globals; initialize via constructors with injected deps.
  - Propagate `context.Context` for timeouts, cancellations, and request data.
  - Manage goroutines securely using channels or sync tools for shared data.
  - Always defer resource cleanup to prevent leaks.

- **Security & Fault Tolerance Measures**
  - Validate and sanitize all external inputs thoroughly.
  - Secure JWT handling, cookies, and configs with best defaults.
  - Enforce strict permissions for sensitive tasks.
  - Add retries with backoff, timeouts to outbound requests.
  - Deploy circuit breakers and rate limits; use Redis for cluster-wide limiting.

- **Comprehensive Testing Approach**
  - Employ table-driven unit tests run in parallel for speed.
  - Mock interfaces simply with tools or custom fakes.
  - Isolate quick unit tests from integration/E2E suites.
  - Cover all public APIs with tests verifying behavior; aim high with `go test -cover`.

- **Documentation & Code Quality**
  - Add GoDoc comments to public APIs and packages.
  - Create focused READMEs per service, plus CONTRIBUTING.md and ARCHITECTURE.md.
  - Standardize with `go fmt`, `goimports`, and `golangci-lint`.

- **Observability Using OpenTelemetry**
  - Integrate tracing, metrics, and structured logs via OpenTelemetry.
  - Create and propagate spans across HTTP/gRPC/DB/external calls with context.
  - Use `otel.Tracer` for spans, `otel.Meter` for metrics; log trace IDs for correlation.
  - Export to collectors like Jaeger or Prometheus.
  - Instrument middleware for auto-tracing endpoints.
  - Track SLIs (e.g., p99 latency <300ms) with Prometheus/Grafana; alert on anomalies.
  - Emit JSON logs with request/trace IDs; minimize label cardinality.

- **Performance & Concurrency Optimization**
  - Benchmark regularly; profile before optimizing allocations.
  - Instrument hotspots like DB queries and computations.
  - Cancel goroutines via context to prevent leaks.

- **Tooling & Dependency Management**
  - Stick to stdlib when possible; choose stable deps.
  - Use Go modules with version pinning for builds.
  - Embed linting, tests, security scans in CI/CD.

Comments

More Rules

View all
AI/ML

GLM-4.7 Optimized Config & System Prompt Designer

Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

C
Community
AI/ML

GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

C
Community
AI/ML

GLM-4.7 Optimized Coding Agent

This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

C
Community
DevOps

Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

C
Claude Directory
AI/ML

Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

C
Community
Database

PostgreSQL Best Practices: Expert Subagent Guide

Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

C
Claude Directory