go-code-review
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGo Code Review
Go代码审查
Quick Reference
快速参考
| Issue Type | Reference |
|---|---|
| Missing error checks, wrapped errors | references/error-handling.md |
| Race conditions, channel misuse | references/concurrency.md |
| Interface pollution, naming | references/interfaces.md |
| Resource leaks, defer misuse | references/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 immediately after creation
defer - No goroutine leaks (channels closed, contexts canceled)
- Interfaces defined by consumers, not producers
- Interface names end in (Reader, Writer, Handler)
-er - 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泄漏(通道已关闭、上下文已取消)
- 接口由消费者而非生产者定义
- 接口名称以结尾(如Reader、Writer、Handler)
-er - 导出名称带有文档注释
- 超过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
审查问题
- Are all error returns checked and wrapped?
- Are goroutines properly managed with context cancellation?
- Are resources (files, connections) closed with defer?
- Are interfaces minimal and defined where used?
- 所有错误返回是否均已检查并包装?
- goroutine是否通过上下文取消进行了妥善管理?
- 资源(文件、连接)是否使用defer关闭?
- 接口是否简洁且在使用处定义?
Valid Patterns (Do NOT Flag)
有效模式(无需标记)
These patterns are acceptable and should NOT be flagged as issues:
- with reason comment - Intentionally ignored errors with explanation
_ = errgo_ = conn.Close() // Best effort cleanup, already handling primary error - Empty interface - For truly generic code (pre-generics codebases)
interface{} - 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
- directives with reason - Acceptable when accompanied by explanation
//nolintgo//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)
以下模式为可接受情况,无需标记为问题:
- 带有原因注释的- 有明确说明的故意忽略错误
_ = errgo_ = conn.Close() // Best effort cleanup, already handling primary error - 空接口- 用于真正的通用代码(泛型出现前的代码库)
interface{} - 短函数中的裸返回 - 在少于5行且使用命名返回值的函数中可接受
- 未关闭的通道 - 当消费者通过上下文取消而非通道关闭停止时
- 保护结构体字段的互斥锁 - 即使仅通过方法访问,这也是正确的封装方式
- 带有原因的指令 - 附带说明时可接受
//nolintgo//nolint:errcheck // Error logged but not returned per API contract - 循环中的defer - 当有意进行函数作用域清理时(例如批量处理文件)
Context-Sensitive Rules
上下文敏感规则
Only flag these issues when the specific conditions apply:
| Issue | Flag ONLY IF |
|---|---|
| Missing error check | Error return is actionable (can retry, log, or propagate) |
| Goroutine leak | No context cancellation path exists for the goroutine |
| Missing defer | Resource isn't explicitly closed before next acquisition or return |
| Interface pollution | Interface 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。