go-dev
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGo 开发规范
Go Development Guidelines
参考来源: Effective Go、Go Code Review Comments、uber-go/guide
Reference Sources: Effective Go, Go Code Review Comments, uber-go/guide
工具链
Toolchain
bash
goimports -w . # 格式化并整理 import
go vet ./... # 静态分析
golangci-lint run # 综合检查
go test -v -race -cover ./... # 测试(含竞态检测和覆盖率)bash
goimports -w . # Format and organize imports
go vet ./... # Static analysis
golangci-lint run # Comprehensive check
go test -v -race -cover ./... # Testing (including race detection and coverage)命名约定
Naming Conventions
| 类型 | 规则 | 示例 |
|---|---|---|
| 包名 | 小写单词,不用下划线 | |
| 变量/函数 | 驼峰命名,缩写词一致大小写 | |
| 常量 | 导出用驼峰,私有可驼峰或全大写 | |
| 接口 | 单方法用方法名+er | |
禁止: , , 等无意义包名
commonutilbase| Type | Rule | Example |
|---|---|---|
| Package Name | Lowercase words, no underscores | |
| Variable/Function | CamelCase, consistent case for abbreviations | |
| Constant | Exported ones use CamelCase, private ones can use CamelCase or all uppercase | |
| Interface | Single-method interfaces use method name + er | |
Prohibited: Meaningless package names like , ,
commonutilbaseimport 顺序
Import Order
go
import (
"context" // 标准库
"fmt"
"github.com/gin-gonic/gin" // 第三方库
"project/internal/model" // 项目内部
)go
import (
"context" // Standard library
"fmt"
"github.com/gin-gonic/gin" // Third-party libraries
"project/internal/model" // Internal project code
)错误处理
Error Handling
必须处理错误,不能忽略:
go
// ✅ 好:添加上下文
if err != nil {
return fmt.Errorf("failed to query user %d: %w", userID, err)
}
// ❌ 差:忽略错误
result, _ := doSomething()错误包装: 使用 保留错误链,用 / 检查
%werrors.Is()errors.As()Errors must be handled, cannot be ignored:
go
// ✅ Good: Add context
if err != nil {
return fmt.Errorf("failed to query user %d: %w", userID, err)
}
// ❌ Bad: Ignore error
result, _ := doSomething()Error Wrapping: Use to preserve error chain, use / for checking
%werrors.Is()errors.As()并发编程
Concurrent Programming
基本原则:
- 优先使用 channel 通信
- 启动 goroutine 前考虑:谁来等待它?怎么停止它?
- 使用 控制生命周期
context.Context
go
// ✅ 好:使用 context 控制
func process(ctx context.Context) error {
done := make(chan error, 1)
go func() { done <- doWork() }()
select {
case err := <-done:
return err
case <-ctx.Done():
return ctx.Err()
}
}数据竞争: 使用 检测
go test -raceBasic Principles:
- Prioritize using channel communication
- Before starting a goroutine, consider: Who will wait for it? How to stop it?
- Use to control lifecycle
context.Context
go
// ✅ Good: Control with context
func process(ctx context.Context) error {
done := make(chan error, 1)
go func() { done <- doWork() }()
select {
case err := <-done:
return err
case <-ctx.Done():
return ctx.Err()
}
}Data Races: Use to detect
go test -race测试规范
Testing Specifications
go
// 表驱动测试
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
expected int
}{
{"positive", 1, 2, 3},
{"zero", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.a, tt.b)
if got != tt.expected {
t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.expected)
}
})
}
}go
// Table-driven test
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
expected int
}{
{"positive", 1, 2, 3},
{"zero", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.a, tt.b)
if got != tt.expected {
t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.expected)
}
})
}
}性能优化
Performance Optimization
| 陷阱 | 解决方案 |
|---|---|
| 循环中拼接字符串 | 使用 |
| 未预分配 slice | |
| N+1 查询 | 批量查询 + 预加载 |
| 无限制并发 | 使用 semaphore 或 worker pool |
bash
undefined| Pitfall | Solution |
|---|---|
| String concatenation in loops | Use |
| Unpreallocated slice | |
| N+1 queries | Batch queries + preloading |
| Unbounded concurrency | Use semaphore or worker pool |
bash
undefined性能分析
Performance analysis
go test -cpuprofile=cpu.prof -bench=.
go tool pprof cpu.prof
---go test -cpuprofile=cpu.prof -bench=.
go tool pprof cpu.prof
---项目结构
Project Structure
project/
├── cmd/ # 可执行文件入口
├── internal/ # 私有代码
│ ├── handler/
│ ├── service/
│ ├── repository/
│ └── model/
├── pkg/ # 公共代码
├── go.mod
└── go.sumproject/
├── cmd/ # Executable entry points
├── internal/ # Private code
│ ├── handler/
│ ├── service/
│ ├── repository/
│ └── model/
├── pkg/ # Public code
├── go.mod
└── go.sum详细参考
Detailed References
完整规范见 ,包含:
references/go-style.md- 完整命名约定和示例
- 详细错误处理模式
- 并发编程最佳实践
- 测试辅助函数写法
- 性能优化工具使用
📋 本回复遵循:- [具体章节]go-dev
Complete guidelines can be found in , including:
references/go-style.md- Complete naming conventions and examples
- Detailed error handling patterns
- Best practices for concurrent programming
- Writing test helper functions
- Usage of performance optimization tools
📋 This response follows:- [Specific Section]go-dev