OhMySkills
语言与基础运行时 go-error-handling 更新于 2026-09-11

Go 工程规则

Idiomatic Go 1.22+ error handling — wrapped errors, errors.Is/As and context propagation

技术栈
Go
主题
工程基线
条目
9 条
版本
v0.2.0

✓ 应当这么做

  • ✓ Return wrapped errors with context using `fmt.Errorf("...: %w", err)` and inspect with errors.Is/errors.As
  • ✓ Pass `context.Context` as the first argument and honour cancellation everywhere
  • ✓ Rely on `go vet`, race detection and table-driven tests in the same package

✕ 常见反模式

  • ✕ Comparing error strings instead of sentinel values
  • ✕ Starting goroutines without a lifecycle owner and a shutdown path
  • ✕ Holding locks while calling into I/O or another package

◎ 交付前验证

  • ◎ `go vet ./...` and `go test -race ./...` pass
  • ◎ No leaked goroutines after the new code path completes
  • ◎ Exported API changes documented in the package comment

同分类条目

32 篇

同一技术栈的其他条目