go-review

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Go — Code Review

Go — 代码审查

Core Principles

核心原则

  • Catch what the compiler can't. Focus on design decisions — error strategies, goroutine lifecycles, API evolution safety.
  • Flag the irreversible. Exported APIs, interface contracts, and error types ship forever. These deserve the most scrutiny.
  • Flag inconsistency. Style violations compound. One embedded mutex today becomes ten next quarter.
  • 捕捉编译器无法检测的问题。聚焦于设计决策——错误处理策略、goroutine生命周期、API演进的安全性。
  • 标记不可逆转的内容。导出的API、接口契约和错误类型一旦发布便会长期存在,这些内容需要最严格的审查。
  • 标记不一致的问题。风格违规会不断累积。今天嵌入一个互斥锁,下个季度可能就会变成十个。

Reference Index

参考索引

ReferenceTopics
api-designExport audit, signature evolution, parameter/result object usage
errorsLog-and-return, missing
%w
, strategy consistency, sentinel/structured misuse
interfacesCompile-time checks, bloat detection, pointer-to-interface, driver pattern
concurrencyEmbedded mutexes, goroutine leaks, WaitGroup races, unbounded spawning
safetyBare type assertions, missing defer, panic in library, boundary copies
performance
fmt.Sprint
for conversions, missing pre-allocation, string concatenation
code-styleNaming, declarations, organization — combined style checks
testingTable-driven tests, assert vs require, bloated tables, goleak
functional-optionsExposed options struct, missing defaults, missing WithX
loggingfmt.Sprintf in logs, wrong level, raw structs, global logger in library
deterministic-simulation-testingDirect time/rand/os calls, non-deterministic maps, missing seed logging, over-abstracted DST
参考链接主题
api-design导出审计、签名演进、参数/结果对象的使用
errors日志并返回、缺失
%w
、策略一致性、哨兵/结构化错误误用
interfaces编译时检查、冗余检测、指针转接口、驱动模式
concurrency嵌入互斥锁、goroutine泄漏、WaitGroup竞争、无限制生成goroutine
safety裸类型断言、缺失defer、库中触发panic、边界拷贝
performance使用
fmt.Sprint
进行类型转换、缺失预分配、字符串拼接
code-style命名、声明、组织——综合风格检查
testing表驱动测试、assert与require的区别、冗余测试表、goleak工具
functional-options暴露选项结构体、缺失默认值、缺少WithX方法
logging日志中使用fmt.Sprintf、错误日志级别、原始结构体、库中使用全局日志器
deterministic-simulation-testing直接调用time/rand/os、非确定性映射、缺失种子日志、过度抽象的确定性模拟测试

When to Apply

适用场景

Apply during:
  • Code reviews of Go pull requests
  • Auditing existing Go packages
  • Pre-merge review of new public APIs
Do NOT apply to non-Go files.
适用于:
  • Go拉取请求的代码审查
  • 现有Go包的审计
  • 新公共API合并前的审查
请勿应用于非Go文件。

Quick Reference

速查指南

Errors: Flag log-and-return. Flag missing
%w
. Flag capitalized messages. Flag
%s
where
%q
needed. Flag mixed strategies. Flag
==
/type-assertion checks.
Interfaces: Flag missing
var _ I = (*T)(nil)
. Flag
*Interface
params. Flag >3 methods. Flag embedded interfaces. Flag missing driver pattern.
Concurrency: Flag embedded mutexes. Flag channel size >1. Flag fire-and-forget goroutines. Flag missing
ctx.Done()
. Flag
wg.Add
inside goroutine.
Safety: Flag bare type assertions. Flag missing defer. Flag panic in library. Flag
os.Exit
outside main. Flag uncopied boundaries.
Performance: Flag
fmt.Sprint
. Flag
[]byte
string
in loops. Flag missing pre-allocation. Flag
+
concatenation in loops.
Style: Flag generic package names. Flag wrong import order. Flag deep nesting. Flag positional struct fields.
Testing: Flag non-table-driven tests. Flag
assert
for preconditions. Flag >10 case tables without split.
Options: Flag exported options struct. Flag missing defaults. Flag mixed options + param objects.
Logging: Flag
fmt.Sprintf
in log args. Flag
err.Error()
as message. Flag wrong log level. Flag raw structs without
LogValuer
. Flag global logger in library. Flag missing
*Context
variant.
DST: Flag direct
time.Now()
. Flag global
rand.*
. Flag direct
os.*
I/O. Flag non-deterministic map iteration. Flag missing seed logging. Flag over-abstracted Clock/FS interfaces where function types suffice.
错误处理:标记“日志并返回”的情况。标记缺失
%w
的情况。标记大写开头的错误消息。标记应使用
%q
却使用
%s
的情况。标记混合的错误处理策略。标记使用
==
/类型断言检查错误的情况。
接口:标记缺失
var _ I = (*T)(nil)
的情况。标记
*Interface
类型的参数。标记包含超过3个方法的接口。标记嵌入的接口。标记未使用驱动模式的情况。
并发:标记嵌入的互斥锁。标记通道大小大于1的情况。标记“一触发就不管”的goroutine。标记缺失
ctx.Done()
的情况。标记在goroutine内部调用
wg.Add
的情况。
安全性:标记裸类型断言。标记缺失defer的情况。标记库中触发panic的情况。标记在main函数外调用
os.Exit
的情况。标记未拷贝边界的情况。
性能:标记使用
fmt.Sprint
的情况。标记循环中进行
[]byte
string
转换的情况。标记缺失预分配的情况。标记循环中使用
+
进行字符串拼接的情况。
风格:标记通用的包名。标记错误的导入顺序。标记深层嵌套的代码。标记使用位置参数的结构体字段。
测试:标记非表驱动的测试。标记对前置条件使用
assert
的情况。标记包含超过10个用例却未拆分的测试表。
函数选项:标记暴露的选项结构体。标记缺失默认值的情况。标记混合使用选项与参数对象的情况。
日志记录:标记日志参数中使用
fmt.Sprintf
的情况。标记将
err.Error()
作为日志消息的情况。标记错误的日志级别。标记未使用
LogValuer
的原始结构体。标记库中使用全局日志器的情况。标记缺失
*Context
变体的情况。
确定性模拟测试(DST):标记直接调用
time.Now()
的情况。标记全局使用
rand.*
的情况。标记直接调用
os.*
进行I/O的情况。标记非确定性的映射迭代。标记缺失种子日志的情况。标记在可用函数类型的场景下过度抽象Clock/FS接口的情况。