go-code-review
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGo Code Review Checklist
Go代码审查清单
Based on Go Wiki CodeReviewComments. This checklist provides quick review points with references to detailed skills.
基于Go Wiki CodeReviewComments。 本清单提供快速审查要点,并关联详细技能文档。
Formatting
格式规范
- gofmt: Code is formatted with or
gofmt→ go-lintinggoimports
- gofmt: 代码已使用或
gofmt格式化 → go-lintinggoimports
Documentation
文档规范
- Comment sentences: Comments are full sentences starting with the name being described, ending with a period → go-documentation
- Doc comments: All exported names have doc comments; non-trivial unexported declarations too → go-documentation
- Package comments: Package comment appears adjacent to package clause with no blank line → go-documentation
- Named result parameters: Only used when they clarify meaning (e.g., multiple same-type returns), not just to enable naked returns → go-documentation
- 注释语句: 注释为完整句子,以被描述对象的名称开头,以句点结尾 → go-documentation
- 文档注释: 所有导出名称均有文档注释;非平凡的未导出声明也需添加 → go-documentation
- 包注释: 包注释紧邻package子句,中间无空行 → go-documentation
- 命名结果参数: 仅在能明确含义时使用(例如多个同类型返回值),而非仅为支持裸返回 → go-documentation
Error Handling
错误处理
- Handle errors: No discarded errors with ; handle, return, or (exceptionally) panic → go-error-handling
_ - Error strings: Lowercase, no punctuation (unless starting with proper noun/acronym) → go-error-handling
- In-band errors: No magic values (-1, "", nil); use multiple returns with error or ok bool → go-error-handling
- Indent error flow: Handle errors first and return; keep normal path at minimal indentation → go-error-handling
- 错误处理: 不使用丢弃错误;需处理、返回或(特殊情况)触发panic → go-error-handling
_ - 错误字符串: 小写格式,无标点符号(除非以专有名词或缩写开头) → go-error-handling
- 带内错误: 不使用魔法值(-1、""、nil);使用多返回值搭配error或ok布尔值 → go-error-handling
- 错误流缩进: 优先处理错误并返回;保持正常逻辑路径的缩进最少 → go-error-handling
Naming
命名规范
- MixedCaps: Use or
MixedCaps, never underscores; unexported ismixedCapsnotmaxLength→ go-namingMAX_LENGTH - Initialisms: Keep consistent case: /
URL,url/ID,id/HTTP(e.g.,http,ServeHTTP) → go-namingxmlHTTPRequest - Variable names: Short names for limited scope (,
i,r); longer names for wider scope → go-namingc - Receiver names: One or two letter abbreviation of type (for
c); noClient,this,self; consistent across methods → go-namingme - Package names: No stuttering (use not
chubby.File); avoidchubby.ChubbyFile,util,common→ go-packagesmisc
- 混合大小写: 使用或
MixedCaps,绝不使用下划线;未导出名称采用mixedCaps而非maxLength→ go-namingMAX_LENGTH - 首字母缩写: 大小写保持一致:/
URL、url/ID、id/HTTP(例如http、ServeHTTP) → go-namingxmlHTTPRequest - 变量命名: 作用域有限的变量使用短名称(、
i、r);作用域广的变量使用长名称 → go-namingc - 接收器名称: 使用类型的一或两个字母缩写(对应
Client);禁止使用c、this、self;在所有方法中保持一致 → go-namingme - 包命名: 避免重复(使用而非
chubby.File);避免使用chubby.ChubbyFile、util、common→ go-packagesmisc
Concurrency
并发处理
- Goroutine lifetimes: Clear when/whether goroutines exit; document if not obvious → go-concurrency
- Synchronous functions: Prefer sync over async; let callers add concurrency if needed → go-concurrency
- Contexts: First parameter; not in structs; no custom Context types; pass even if you think you don't need to → go-context
- Goroutine生命周期: 明确Goroutine的退出时机/是否退出;若不明显则需文档说明 → go-concurrency
- 同步函数: 优先选择同步而非异步;让调用者自行添加并发逻辑(若需要) → go-concurrency
- Context: 作为第一个参数;不放入结构体中;不自定义Context类型;即使认为不需要也需传递 → go-context
Interfaces
接口设计
- Interface location: Define in consumer package, not implementor; return concrete types from producers → go-interfaces
- No premature interfaces: Don't define before used; don't define "for mocking" on implementor side → go-interfaces
- Receiver type: Use pointer if mutating, has sync fields, or is large; value for small immutable types; don't mix → go-interfaces
- 接口位置: 在消费方包中定义,而非实现方;生产者返回具体类型 → go-interfaces
- 避免过早定义接口: 不要在使用前定义接口;不要在实现方为“便于Mock”定义接口 → go-interfaces
- 接收器类型: 若需修改值、包含同步字段或类型较大则使用指针;小型不可变类型使用值类型;不要混合使用 → go-interfaces
Data Structures
数据结构
- Empty slices: Prefer (nil) over
var t []string(non-nil zero-length) → go-data-structurest := []string{} - Copying: Be careful copying structs with pointer/slice fields; don't copy methods' receivers by value → go-data-structures
*T
- 空切片: 优先使用(nil)而非
var t []string(非nil的零长度切片) → go-data-structurest := []string{} - 拷贝操作: 拷贝包含指针/切片字段的结构体时需谨慎;不要按值拷贝方法的接收器 → go-data-structures
*T
Security
安全规范
- Crypto rand: Use for keys, not
crypto/rand→ go-defensivemath/rand - Don't panic: Use error returns for normal error handling; panic only for truly exceptional cases → go-defensive
- 加密随机数: 生成密钥时使用而非
crypto/rand→ go-defensivemath/rand - 避免panic: 正常错误处理使用返回错误的方式;仅在真正异常的情况下触发panic → go-defensive
Style
代码风格
- Line length: No rigid limit, but avoid uncomfortably long lines; break by semantics, not arbitrary length → go-style-core
- Naked returns: Only in short functions; explicit returns in medium/large functions → go-style-core
- Pass values: Don't use pointers just to save bytes; pass not
stringfor small fixed-size types → go-performance*string
- 行长度: 无严格限制,但避免过长的行;按语义拆分而非按任意长度拆分 → go-style-core
- 裸返回: 仅在短函数中使用;中大型函数使用显式返回 → go-style-core
- 值传递: 不要仅为节省内存使用指针;小型固定大小类型传递而非
string→ go-performance*string
Imports
导入规范
- Import groups: Standard library first, then blank line, then external packages → go-packages
- Import renaming: Avoid unless collision; rename local/project-specific import on collision → go-packages
- Import blank: only in main package or tests → go-packages
import _ "pkg" - Import dot: Only for circular dependency workarounds in tests → go-packages
- 导入分组: 先导入标准库,空行后再导入外部包 → go-packages
- 导入重命名: 除非存在冲突否则避免重命名;仅在本地/项目特定导入冲突时重命名 → go-packages
- 空白导入: 仅在main包或测试中使用 → go-packages
import _ "pkg" - 点导入: 仅用于测试中的循环依赖解决方法 → go-packages
Testing
测试规范
- Examples: Include runnable functions or tests demonstrating usage → go-documentation
Example - Useful test failures: Messages include what was wrong, inputs, got, and want; order is → go-testing
got != want
- 示例代码: 包含可运行的函数或测试用例以演示用法 → go-documentation
Example - 有用的测试失败信息: 信息需包含错误内容、输入值、实际结果和预期结果;格式为→ go-testing
got != want
Quick Review Commands
快速审查命令
bash
undefinedbash
undefinedFormat and organize imports
格式化并整理导入
goimports -w .
goimports -w .
Run linter suite
运行检查器套件
golangci-lint run
golangci-lint run
Check for common issues
检查常见问题
go vet ./...
---go vet ./...
---See Also
另请参阅
- go-linting: Automated tooling for style enforcement
- go-style-core: Core Go style principles
- go-documentation: Documentation and comment standards
- go-error-handling: Error handling patterns
- go-naming: Naming conventions
- go-packages: Package design and imports
- go-interfaces: Interface design patterns
- go-concurrency: Concurrency patterns
- go-context: Context usage patterns
- go-data-structures: Data structure idioms
- go-defensive: Defensive programming
- go-testing: Testing patterns
- go-performance: Performance considerations
- go-linting: 用于风格强制的自动化工具
- go-style-core: Go核心风格原则
- go-documentation: 文档与注释标准
- go-error-handling: 错误处理模式
- go-naming: 命名规范
- go-packages: 包设计与导入规范
- go-interfaces: 接口设计模式
- go-concurrency: 并发模式
- go-context: Context使用模式
- go-data-structures: 数据结构惯用写法
- go-defensive: 防御式编程
- go-testing: 测试模式
- go-performance: 性能考量