AgentSkillsCN

Architecture

在 Golang 中遵循结构化设计、整洁架构以及项目布局的规范。

SKILL.md
--- frontmatter
name: Architecture
description: Standards for structural design, Clean Architecture, and project layout in Golang.
metadata:
  labels: [golang, architecture, clean-arch, project-layout, ddd]
  triggers:
    files: ['go.mod', 'internal/**']
    keywords:
      [architecture, structure, folder layout, clean arch, dependency injection]

Golang Architecture Standards

Priority: P0 (CRITICAL)

Principles

  • Clean Architecture: Separate concerns. Inner layers (Domain) rely on nothing. Outer layers (Adapters) rely on Inner.
  • Project Layout: Follow standard Go project layout (cmd, internal, pkg).
  • Dependency Injection: Explicitly pass dependencies via constructors. Avoid global singletons.
  • Package Oriented Design: Organize by feature/domain, not by layer (avoid controllers/, services/ at root).
  • Interface Segregation: Define interfaces where they are used (Consumer implementation).

Standard Project Layout

  • cmd/: Application entry points (e.g., cmd/server/main.go).
  • internal/: Private application code. Not importable by other modules.
    • internal/domain/: Entities, Business Rules (Pure Go, no heavy imports).
    • internal/usecase/: Application Logic (Interactors).
    • internal/adapter/: Database, HTTP, GRPC adapters.
  • pkg/: Library code ok to use by external applications.
  • configs/: Configuration files.

Guidelines

  • Use Constructors: NewService(repo Repository) *Service.
  • Inversion of Control: Service depends on Repository interface, not SQLRepository struct.
  • Wire up in Main: Main function composes the dependency graph.

References