Go 微服务 — 项目 CLAUDE.md¶
适用于使用 PostgreSQL、gRPC 和 Docker 的 Go 微服务的真实示例。 请将其复制到项目根目录,并根据您的服务进行自定义。
项目概览¶
技术栈: Go 1.22+、PostgreSQL、gRPC + REST(grpc-gateway)、Docker、sqlc(SQL 类型安全)、Wire(依赖注入)
架构: 采用 Clean architecture,包含 domain、repository、service 和 handler 层。gRPC 作为主要传输方式,并为外部客户端提供 REST 网关。
关键规则¶
Go 约定¶
- 遵循 Effective Go 和 Go Code Review Comments 指南
- 使用
errors.New/fmt.Errorf配合%w进行错误包装——切勿在错误中进行字符串匹配 - 不使用
init()函数——在main()或构造函数中进行显式初始化 - 不使用可变全局状态——通过构造函数传递依赖
- Context 应为第一个参数,并在所有层中传播
数据库¶
- 所有查询均以纯 SQL 形式放在
queries/中——sqlc 会生成类型安全的 Go 代码 - 迁移文件放在
migrations/中,并使用 golang-migrate——切勿直接修改数据库 - 对于多步操作,通过
pgx.Tx使用事务 - 所有查询都必须使用参数化占位符(
$1、$2)——切勿使用字符串格式化
错误处理¶
- 返回错误,不要使用 panic——panic 仅用于真正不可恢复的情况
- 为错误添加上下文进行包装:
fmt.Errorf("creating user: %w", err) - 在
domain/errors.go中为业务逻辑定义哨兵错误 - 在 handler 层将领域错误映射为 gRPC 状态码
// Domain layer — sentinel errors
var (
ErrUserNotFound = errors.New("user not found")
ErrEmailTaken = errors.New("email already registered")
)
// Handler layer — map to gRPC status
func toGRPCError(err error) error {
switch {
case errors.Is(err, domain.ErrUserNotFound):
return status.Error(codes.NotFound, err.Error())
case errors.Is(err, domain.ErrEmailTaken):
return status.Error(codes.AlreadyExists, err.Error())
default:
return status.Error(codes.Internal, "internal error")
}
}
代码风格¶
- 代码或注释中不使用表情符号
- 导出的类型和函数必须有文档注释
- 保持函数在 50 行以内——提取辅助函数
- 对于所有包含多个用例的逻辑,使用表驱动测试
- 对于信号通道,优先使用
struct{},而不是bool
文件结构¶
cmd/
server/
main.go # Entrypoint, Wire injection, graceful shutdown
internal/
domain/ # Business types and interfaces
user.go # User entity and repository interface
errors.go # Sentinel errors
service/ # Business logic
user_service.go
user_service_test.go
repository/ # Data access (sqlc-generated + custom)
postgres/
user_repo.go
user_repo_test.go # Integration tests with testcontainers
handler/ # gRPC + REST handlers
grpc/
user_handler.go
rest/
user_handler.go
config/ # Configuration loading
config.go
proto/ # Protobuf definitions
user/v1/
user.proto
queries/ # SQL queries for sqlc
user.sql
migrations/ # Database migrations
001_create_users.up.sql
001_create_users.down.sql
关键模式¶
仓储接口¶
type UserRepository interface {
Create(ctx context.Context, user *User) error
FindByID(ctx context.Context, id uuid.UUID) (*User, error)
FindByEmail(ctx context.Context, email string) (*User, error)
Update(ctx context.Context, user *User) error
Delete(ctx context.Context, id uuid.UUID) error
}
使用依赖注入的服务¶
type UserService struct {
repo domain.UserRepository
hasher PasswordHasher
logger *slog.Logger
}
func NewUserService(repo domain.UserRepository, hasher PasswordHasher, logger *slog.Logger) *UserService {
return &UserService{repo: repo, hasher: hasher, logger: logger}
}
func (s *UserService) Create(ctx context.Context, req CreateUserRequest) (*domain.User, error) {
existing, err := s.repo.FindByEmail(ctx, req.Email)
if err != nil && !errors.Is(err, domain.ErrUserNotFound) {
return nil, fmt.Errorf("checking email: %w", err)
}
if existing != nil {
return nil, domain.ErrEmailTaken
}
hashed, err := s.hasher.Hash(req.Password)
if err != nil {
return nil, fmt.Errorf("hashing password: %w", err)
}
user := &domain.User{
ID: uuid.New(),
Name: req.Name,
Email: req.Email,
Password: hashed,
}
if err := s.repo.Create(ctx, user); err != nil {
return nil, fmt.Errorf("creating user: %w", err)
}
return user, nil
}
表驱动测试¶
func TestUserService_Create(t *testing.T) {
tests := []struct {
name string
req CreateUserRequest
setup func(*MockUserRepo)
wantErr error
}{
{
name: "valid user",
req: CreateUserRequest{Name: "Alice", Email: "alice@example.com", Password: "secure123"},
setup: func(m *MockUserRepo) {
m.On("FindByEmail", mock.Anything, "alice@example.com").Return(nil, domain.ErrUserNotFound)
m.On("Create", mock.Anything, mock.Anything).Return(nil)
},
wantErr: nil,
},
{
name: "duplicate email",
req: CreateUserRequest{Name: "Alice", Email: "taken@example.com", Password: "secure123"},
setup: func(m *MockUserRepo) {
m.On("FindByEmail", mock.Anything, "taken@example.com").Return(&domain.User{}, nil)
},
wantErr: domain.ErrEmailTaken,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
repo := new(MockUserRepo)
tt.setup(repo)
svc := NewUserService(repo, &bcryptHasher{}, slog.Default())
_, err := svc.Create(context.Background(), tt.req)
if tt.wantErr != nil {
assert.ErrorIs(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
}
})
}
}
环境变量¶
# Database
DATABASE_URL=postgres://user:pass@localhost:5432/myservice?sslmode=disable
# gRPC
GRPC_PORT=50051
REST_PORT=8080
# Auth
JWT_SECRET= # Load from vault in production
TOKEN_EXPIRY=24h
# Observability
LOG_LEVEL=info # debug, info, warn, error
OTEL_ENDPOINT= # OpenTelemetry collector
测试策略¶
测试命令¶
# Unit tests (fast, no external deps)
go test ./internal/... -short -count=1
# Integration tests (requires Docker for testcontainers)
go test ./internal/repository/... -count=1 -timeout 120s
# All tests with coverage
go test ./... -coverprofile=coverage.out -count=1
go tool cover -func=coverage.out # summary
go tool cover -html=coverage.out # browser
# Race detector
go test ./... -race -count=1
ECC 工作流¶
# Planning
/plan "Add rate limiting to user endpoints"
# Development
/go-test # TDD with Go-specific patterns
# Review
/go-review # Go idioms, error handling, concurrency
/security-scan # Secrets and vulnerabilities
# Before merge
go vet ./...
staticcheck ./...
Git 流程¶
feat:新功能,fix:缺陷修复,refactor:代码重构- 从
main创建功能分支,必须通过 PR - CI:
go vet、staticcheck、go test -race、golangci-lint - 部署:在 CI 中生成 Docker 镜像并发布到 Kubernetes
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 ev/ECC