go-code-review

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Go 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
    gofmt
    or
    goimports
    go-linting

  • gofmt: 代码已使用
    gofmt
    goimports
    格式化 → go-linting

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
    MixedCaps
    or
    mixedCaps
    , never underscores; unexported is
    maxLength
    not
    MAX_LENGTH
    go-naming
  • Initialisms: Keep consistent case:
    URL
    /
    url
    ,
    ID
    /
    id
    ,
    HTTP
    /
    http
    (e.g.,
    ServeHTTP
    ,
    xmlHTTPRequest
    ) → go-naming
  • Variable names: Short names for limited scope (
    i
    ,
    r
    ,
    c
    ); longer names for wider scope → go-naming
  • Receiver names: One or two letter abbreviation of type (
    c
    for
    Client
    ); no
    this
    ,
    self
    ,
    me
    ; consistent across methods → go-naming
  • Package names: No stuttering (use
    chubby.File
    not
    chubby.ChubbyFile
    ); avoid
    util
    ,
    common
    ,
    misc
    go-packages

  • 混合大小写: 使用
    MixedCaps
    mixedCaps
    ,绝不使用下划线;未导出名称采用
    maxLength
    而非
    MAX_LENGTH
    go-naming
  • 首字母缩写: 大小写保持一致:
    URL
    /
    url
    ID
    /
    id
    HTTP
    /
    http
    (例如
    ServeHTTP
    xmlHTTPRequest
    ) → go-naming
  • 变量命名: 作用域有限的变量使用短名称(
    i
    r
    c
    );作用域广的变量使用长名称 → go-naming
  • 接收器名称: 使用类型的一或两个字母缩写(
    Client
    对应
    c
    );禁止使用
    this
    self
    me
    ;在所有方法中保持一致 → go-naming
  • 包命名: 避免重复(使用
    chubby.File
    而非
    chubby.ChubbyFile
    );避免使用
    util
    common
    misc
    go-packages

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
    var t []string
    (nil) over
    t := []string{}
    (non-nil zero-length) → go-data-structures
  • Copying: Be careful copying structs with pointer/slice fields; don't copy
    *T
    methods' receivers by value → go-data-structures

  • 空切片: 优先使用
    var t []string
    (nil)而非
    t := []string{}
    (非nil的零长度切片) → go-data-structures
  • 拷贝操作: 拷贝包含指针/切片字段的结构体时需谨慎;不要按值拷贝
    *T
    方法的接收器 → go-data-structures

Security

安全规范

  • Crypto rand: Use
    crypto/rand
    for keys, not
    math/rand
    go-defensive
  • Don't panic: Use error returns for normal error handling; panic only for truly exceptional cases → go-defensive

  • 加密随机数: 生成密钥时使用
    crypto/rand
    而非
    math/rand
    go-defensive
  • 避免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
    string
    not
    *string
    for small fixed-size types → go-performance

  • 行长度: 无严格限制,但避免过长的行;按语义拆分而非按任意长度拆分 → go-style-core
  • 裸返回: 仅在短函数中使用;中大型函数使用显式返回 → go-style-core
  • 值传递: 不要仅为节省内存使用指针;小型固定大小类型传递
    string
    而非
    *string
    go-performance

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:
    import _ "pkg"
    only in main package or tests → go-packages
  • Import dot: Only for circular dependency workarounds in tests → go-packages

  • 导入分组: 先导入标准库,空行后再导入外部包 → go-packages
  • 导入重命名: 除非存在冲突否则避免重命名;仅在本地/项目特定导入冲突时重命名 → go-packages
  • 空白导入:
    import _ "pkg"
    仅在main包或测试中使用 → go-packages
  • 点导入: 仅用于测试中的循环依赖解决方法 → go-packages

Testing

测试规范

  • Examples: Include runnable
    Example
    functions or tests demonstrating usage → go-documentation
  • Useful test failures: Messages include what was wrong, inputs, got, and want; order is
    got != want
    go-testing

  • 示例代码: 包含可运行的
    Example
    函数或测试用例以演示用法 → go-documentation
  • 有用的测试失败信息: 信息需包含错误内容、输入值、实际结果和预期结果;格式为
    got != want
    go-testing

Quick Review Commands

快速审查命令

bash
undefined
bash
undefined

Format 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: 性能考量