go-code-review

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Go Code Review

Go代码审查

Quick Reference

快速参考

Issue TypeReference
Missing error checks, wrapped errorsreferences/error-handling.md
Race conditions, channel misusereferences/concurrency.md
Interface pollution, namingreferences/interfaces.md
Resource leaks, defer misusereferences/common-mistakes.md
问题类型参考文档
缺失错误检查、未包装错误references/error-handling.md
竞态条件、通道误用references/concurrency.md
接口冗余、命名问题references/interfaces.md
资源泄漏、defer误用references/common-mistakes.md

Review Checklist

审查检查清单

  • All errors are checked (no
    _ = err
    )
  • Errors wrapped with context (
    fmt.Errorf("...: %w", err)
    )
  • Resources closed with
    defer
    immediately after creation
  • No goroutine leaks (channels closed, contexts canceled)
  • Interfaces defined by consumers, not producers
  • Interface names end in
    -er
    (Reader, Writer, Handler)
  • Exported names have doc comments
  • No naked returns in functions > 5 lines
  • Context passed as first parameter
  • Mutexes protect shared state, not methods
  • 所有错误均已检查(无
    _ = err
    写法)
  • 错误已附带上下文包装(
    fmt.Errorf("...: %w", err)
  • 资源创建后立即使用
    defer
    关闭
  • 无goroutine泄漏(通道已关闭、上下文已取消)
  • 接口由消费者而非生产者定义
  • 接口名称以
    -er
    结尾(如Reader、Writer、Handler)
  • 导出名称带有文档注释
  • 超过5行的函数中无裸返回
  • 上下文作为第一个参数传递
  • 互斥锁保护共享状态而非方法

When to Load References

何时加载参考文档

  • Reviewing error return patterns → error-handling.md
  • Reviewing goroutines/channels → concurrency.md
  • Reviewing type definitions → interfaces.md
  • General Go review → common-mistakes.md
  • 审查错误返回模式 → error-handling.md
  • 审查goroutine/通道 → concurrency.md
  • 审查类型定义 → interfaces.md
  • 常规Go代码审查 → common-mistakes.md

Review Questions

审查问题

  1. Are all error returns checked and wrapped?
  2. Are goroutines properly managed with context cancellation?
  3. Are resources (files, connections) closed with defer?
  4. Are interfaces minimal and defined where used?
  1. 所有错误返回是否均已检查并包装?
  2. goroutine是否通过上下文取消进行了妥善管理?
  3. 资源(文件、连接)是否使用defer关闭?
  4. 接口是否简洁且在使用处定义?

Valid Patterns (Do NOT Flag)

有效模式(无需标记)

These patterns are acceptable and should NOT be flagged as issues:
  • _ = err
    with reason comment
    - Intentionally ignored errors with explanation
    go
    _ = conn.Close() // Best effort cleanup, already handling primary error
  • Empty interface
    interface{}
    - For truly generic code (pre-generics codebases)
  • Naked returns in short functions - Acceptable in functions < 5 lines with named returns
  • Channel without close - When consumer stops via context cancellation, not channel close
  • Mutex protecting struct fields - Even if accessed only via methods, this is correct encapsulation
  • //nolint
    directives with reason
    - Acceptable when accompanied by explanation
    go
    //nolint:errcheck // Error logged but not returned per API contract
  • Defer in loop - When function scope cleanup is intentional (e.g., processing files in batches)
以下模式为可接受情况,无需标记为问题:
  • 带有原因注释的
    _ = err
    - 有明确说明的故意忽略错误
    go
    _ = conn.Close() // Best effort cleanup, already handling primary error
  • 空接口
    interface{}
    - 用于真正的通用代码(泛型出现前的代码库)
  • 短函数中的裸返回 - 在少于5行且使用命名返回值的函数中可接受
  • 未关闭的通道 - 当消费者通过上下文取消而非通道关闭停止时
  • 保护结构体字段的互斥锁 - 即使仅通过方法访问,这也是正确的封装方式
  • 带有原因的
    //nolint
    指令
    - 附带说明时可接受
    go
    //nolint:errcheck // Error logged but not returned per API contract
  • 循环中的defer - 当有意进行函数作用域清理时(例如批量处理文件)

Context-Sensitive Rules

上下文敏感规则

Only flag these issues when the specific conditions apply:
IssueFlag ONLY IF
Missing error checkError return is actionable (can retry, log, or propagate)
Goroutine leakNo context cancellation path exists for the goroutine
Missing deferResource isn't explicitly closed before next acquisition or return
Interface pollutionInterface has > 1 method AND only one consumer exists
仅在特定条件满足时才标记这些问题:
问题仅在以下情况标记
缺失错误检查错误返回可采取行动(可重试、记录或传播)
Goroutine泄漏该goroutine无上下文取消路径
缺失defer资源在下次获取或返回前未显式关闭
接口冗余接口包含>1个方法且仅存在一个消费者

Before Submitting Findings

提交问题前须知

Load and follow review-verification-protocol before reporting any issue.
在报告任何问题前,请加载并遵循review-verification-protocol