Go Backend Microservices

作者 mindrally97184105b5da無授權條款269 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 週前更新

Go backend development best practices for microservices with clean architecture, observability, and production-ready patterns

AI 產生的概覽

提供 Go 微服務開發規範,涵蓋整潔架構、可觀測性、測試與正式環境實務。

功能
提供針對微服務的 Go 後端開發慣例,涵蓋整潔架構分層、專案目錄結構、錯誤處理、情境傳遞、可觀測性、安全性、測試、並行處理、文件以及 CI/CD。它只是純說明性參考,產出的是指導建議而非程式碼或檔案。
適用情境
適合在設計或審查 Go 微服務時,希望統一結構、錯誤處理、追蹤、指標、日誌與測試方面的慣例。也適合為 Go 後端訂定 lint 與 CI 規範。
執行需求
未附任何指令碼或工具,僅為說明性內容。若要落實這些建議,需要 Go 專案,並在相關部分使用 OpenTelemetry、Prometheus、golangci-lint 與測試容器。

Go Backend Development for Microservices

Core Principles

  • Apply Clean Architecture with clear separation into handlers, services, repositories, and domain models
  • Prioritize interface-driven development with explicit dependency injection
  • Write short, focused functions with a single responsibility
  • Ensure safe use of goroutines, and guard shared state with channels or sync primitives

Project Structure

Maintain modular project structure with clear directories:

project/├── cmd/           # Application entry points├── internal/      # Private application code├── pkg/           # Public library code├── api/           # API definitions (OpenAPI, protobuf)├── configs/       # Configuration files└── test/          # Additional test utilities

Error Handling

  • Always check and handle errors explicitly
  • Use wrapped errors for traceability: fmt.Errorf("context: %w", err)
  • Create custom error types for domain-specific errors
  • Return errors up the call stack with appropriate context
  • Log errors at the appropriate level with sufficient context

Context Propagation

  • Use context propagation for request-scoped values, deadlines, and cancellations
  • Pass context as the first parameter to functions
  • Respect context cancellation in long-running operations
  • Set appropriate timeouts for external calls

Observability

Implement OpenTelemetry for comprehensive observability:

Distributed Tracing

  • Add spans for significant operations
  • Propagate trace context across service boundaries
  • Include relevant attributes in spans

Metrics

  • Implement custom metrics for business operations
  • Use standard metric types (counters, gauges, histograms)
  • Export metrics in Prometheus format

Structured Logging

  • Use structured logging with consistent field names
  • Include trace IDs in log entries
  • Log at appropriate levels (debug, info, warn, error)

Security

  • Apply input validation rigorously on all external inputs
  • Use secure defaults for JWT tokens and cookies
  • Implement proper authentication and authorization
  • Sanitize data before logging to avoid leaking sensitive information
  • Use prepared statements for database queries

Testing

Unit Tests

  • Write table-driven unit tests with adequate coverage
  • Use parallel test execution where safe
  • Mock external dependencies using interfaces
  • Focus on testing business logic

Integration Tests

  • Separate integration tests from unit tests
  • Use test containers for database and service dependencies
  • Test actual API endpoints and responses

Concurrency

  • Use goroutines appropriately for concurrent operations
  • Guard shared state with channels or sync primitives
  • Implement proper graceful shutdown
  • Use worker pools for bounded concurrency

Documentation

  • Document with GoDoc-style comments
  • Keep comments up to date with code changes
  • Document public APIs thoroughly
  • Include examples in documentation where helpful

CI/CD Integration

  • Maintain CI integration for linting and testing
  • Use golangci-lint for comprehensive linting
  • Run tests on every pull request
  • Include code coverage reporting

來源與署名

來源:mindrally/skills位於go-backend-microservices提交9718410

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架