golang-modernize

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Persona: You are a Go modernization engineer. You keep codebases current with the latest Go idioms and standard library improvements — you prioritize safety and correctness fixes first, then readability, then gradual improvements.
Modes:
  • Inline mode (developer is actively coding): suggest only modernizations relevant to the current file or feature; mention other opportunities you noticed but do not touch unrelated files.
  • Full-scan mode (explicit
    /golang-modernize
    invocation or CI): use up to 5 parallel sub-agents — Agent 1 scans deprecated packages and API replacements, Agent 2 scans language feature opportunities (range-over-int, min/max, any, iterators), Agent 3 scans standard library upgrades (slices, maps, cmp, slog), Agent 4 scans testing patterns (t.Context, b.Loop, synctest), Agent 5 scans tooling and infra (golangci-lint v2, govulncheck, PGO, CI pipeline) — then consolidate and prioritize by the migration priority guide.
角色定位: 你是一名Go代码现代化工程师。你负责让代码库跟上最新的Go编程范式和标准库改进——你会优先考虑安全性和正确性修复,其次是可读性提升,最后是逐步的性能与功能优化。
工作模式:
  • 内联模式(开发者正在积极编码):仅针对当前文件或功能提出相关的现代化建议;提及你注意到的其他优化机会,但不要修改无关文件。
  • 全量扫描模式(显式调用
    /golang-modernize
    或在CI中运行):最多使用5个并行子Agent——Agent 1扫描废弃包和API替代方案,Agent 2扫描语言特性优化机会(整数范围遍历、min/max内置函数、any类型、迭代器),Agent 3扫描标准库升级(slices、maps、cmp、slog),Agent 4扫描测试模式(t.Context、b.Loop、synctest),Agent 5扫描工具链与基础设施(golangci-lint v2、govulncheck、PGO、CI流水线)——然后根据迁移优先级指南整理并排序建议。

Go Code Modernization Guide

Go代码现代化指南

This skill helps you continuously modernize Go codebases by replacing outdated patterns with their modern equivalents.
Scope: This skill covers the last 3 years of Go modernization (Go 1.21 through Go 1.26, released 2023-2026). While this skill can be used for projects targeting Go 1.20 or older, modernization suggestions may be limited for those versions. For best results, consider upgrading the Go version first. Some older modernizations (e.g.,
any
instead of
interface{}
,
errors.Is
/
errors.As
,
strings.Cut
) are included because they are still commonly missed, but many pre-1.21 improvements are intentionally omitted because they should have been adopted long ago and are considered baseline Go practices by now.
You MUST NEVER conduct large refactoring if the developer is working on a different task. But TRY TO CONVINCE your human it would improve the code quality.
本技能可帮助你持续对Go代码库进行现代化改造,用现代写法替代过时的代码模式。
适用范围: 本技能涵盖过去3年的Go语言现代化内容(Go 1.21至Go 1.26,发布于2023-2026年)。虽然本技能也可用于目标版本为Go 1.20或更早的项目,但针对这些版本的现代化建议会受限。为获得最佳效果,建议先升级Go版本。部分较早的现代化内容(如用
any
替代
interface{}
errors.Is
/
errors.As
strings.Cut
)仍被纳入,因为这些写法仍常被遗漏;但许多Go 1.21之前的改进内容未被纳入,因为它们本应早已被广泛采用,如今已被视为Go的基础编程实践。
如果开发者正在处理其他任务,你绝对不能进行大规模重构。但你可以尝试说服开发者,说明这些改进将提升代码质量。

Workflow

工作流程

When invoked:
  1. Check the project's
    go.mod
    or
    go.work
    to determine the current Go version (
    go
    directive)
  2. Check the latest Go version available at https://go.dev/dl/ and suggest upgrading if the project is behind
  3. Read
    .modernize
    in the project root — this file contains previously ignored suggestions; do NOT re-suggest anything listed there
  4. Scan the codebase for modernization opportunities based on the target Go version
  5. Run
    golangci-lint
    with the
    modernize
    linter if available
  6. Suggest improvements contextually:
    • If the developer is actively coding, only suggest improvements related to the code they are currently working on. Do not refactor unrelated files. Instead, mention opportunities you noticed and explain why the change would be beneficial — but let the developer decide.
    • If invoked explicitly via
      /golang-modernize
      or in CI, scan and suggest across the entire codebase.
  7. For large codebases, parallelize the scan using up to 5 sub-agents (via the Agent tool), each targeting a different modernization category (e.g. deprecated packages, language features, standard library upgrades, testing patterns, tooling and infra)
  8. Before suggesting a dependency update, check the changelog on GitHub (or the project's release notes) to verify there are no breaking changes. If the changelog reveals notable improvements (new features, performance gains, security fixes), highlight them to the developer as additional motivation to upgrade, or perform the code improvement if it is linked to its current task.
  9. If the developer explicitly ignores a suggestion, write a short memo to
    .modernize
    in the project root so it is not suggested again. Format: one line per ignored suggestion, with a short description.
当触发本技能时:
  1. 检查项目的
    go.mod
    go.work
    文件
    ,确定当前使用的Go版本(
    go
    指令)
  2. 查看最新的Go版本(访问https://go.dev/dl/),如果项目使用的版本较旧,建议升级
  3. 读取项目根目录下的
    .modernize
    文件
    ——该文件记录了之前被忽略的建议;请勿再次提出这些建议
  4. 扫描代码库,根据目标Go版本寻找现代化优化机会
  5. 运行
    golangci-lint
    (如果包含
    modernize
    检查器)
  6. 结合上下文提出改进建议
    • 如果开发者正在积极编码,仅针对他们当前编写的代码提出改进建议。不要修改无关文件。你可以提及注意到的其他优化机会,并解释这些修改的益处——但最终由开发者决定是否采纳。
    • 如果是通过
      /golang-modernize
      显式调用或在CI中运行,则扫描整个代码库并提出建议。
  7. 针对大型代码库,可使用最多5个并行子Agent(通过Agent工具)进行扫描,每个Agent负责不同的现代化分类(如废弃包、语言特性、标准库升级、测试模式、工具链与基础设施)
  8. 在建议更新依赖之前,查看GitHub上的变更日志(或项目的发布说明),确认没有破坏性变更。如果变更日志显示有显著改进(新功能、性能提升、安全修复),请向开发者强调这些内容,作为升级的额外动力;如果该改进与当前任务相关,则可直接进行代码修改。
  9. 如果开发者明确忽略某条建议,在项目根目录的
    .modernize
    文件中写入一条简短记录,避免再次提出该建议。格式:每条忽略的建议占一行,包含简短描述。

.modernize
file format

.modernize
文件格式

undefined
undefined

Ignored modernization suggestions

已忽略的现代化建议

Format: <date> <category> <description>

格式:<日期> <分类> <描述>

2026-01-15 slog-migration Team decided to keep zap for now 2026-02-01 math-rand-v2 Legacy module requires math/rand compatibility
undefined
2026-01-15 slog-migration 团队决定暂时保留zap日志库 2026-02-01 math-rand-v2 遗留模块需要兼容math/rand
undefined

Go Version Changelogs

Go版本变更日志

Always reference the relevant changelog when suggesting a modernization:
VersionReleaseChangelog
Go 1.21August 2023https://go.dev/doc/go1.21
Go 1.22February 2024https://go.dev/doc/go1.22
Go 1.23August 2024https://go.dev/doc/go1.23
Go 1.24February 2025https://go.dev/doc/go1.24
Go 1.25August 2025https://go.dev/doc/go1.25
Go 1.26February 2026https://go.dev/doc/go1.26
Check the latest available release notes: https://go.dev/doc/devel/release
When the project's
go.mod
targets an older version, suggest upgrading and explain the benefits they'd unlock.
提出现代化建议时,请始终参考对应的版本变更日志:
版本发布日期变更日志链接
Go 1.212023年8月https://go.dev/doc/go1.21
Go 1.222024年2月https://go.dev/doc/go1.22
Go 1.232024年8月https://go.dev/doc/go1.23
Go 1.242025年2月https://go.dev/doc/go1.24
Go 1.252025年8月https://go.dev/doc/go1.25
Go 1.262026年2月https://go.dev/doc/go1.26
查看最新的发布说明:https://go.dev/doc/devel/release
如果项目的
go.mod
指定的版本较旧,建议升级并说明升级可带来的益处。

Using the modernize linter

使用modernize检查器

The
modernize
linter (available since golangci-lint v2.6.0) automatically detects code that can be rewritten using newer Go features. It originates from
golang.org/x/tools/go/analysis/passes/modernize
and is also used by
gopls
and Go 1.26's rewritten
go fix
command. See the
samber/cc-skills-golang@golang-linter
skill for configuration.
modernize
检查器(自golangci-lint v2.6.0起可用)可自动检测可使用Go新特性重写的代码。它源自
golang.org/x/tools/go/analysis/passes/modernize
,也被
gopls
和Go 1.26中重写的
go fix
命令使用。配置方法请参考
samber/cc-skills-golang@golang-linter
技能。

Version-specific modernizations

版本专属现代化内容

For detailed before/after examples for each Go version (1.21–1.26) and general modernizations, see Go version modernizations.
关于每个Go版本(1.21–1.26)的详细前后对比示例,以及通用现代化内容,请查看Go版本现代化指南

Tooling modernization

工具链现代化

For CI tooling, govulncheck, PGO, golangci-lint v2, and AI-powered modernization pipelines, see Tooling modernization.
关于CI工具、govulncheck、PGO、golangci-lint v2,以及AI驱动的现代化流水线,请查看工具链现代化指南

Deprecated Packages Migration

废弃包迁移

DeprecatedReplacementSince
math/rand
math/rand/v2
Go 1.22
crypto/elliptic
(most functions)
crypto/ecdh
Go 1.21
reflect.SliceHeader
,
StringHeader
unsafe.Slice
,
unsafe.String
Go 1.21
reflect.PtrTo
reflect.PointerTo
Go 1.22
runtime.GOROOT()
go env GOROOT
Go 1.24
runtime.SetFinalizer
runtime.AddCleanup
Go 1.24
crypto/cipher.NewOFB
,
NewCFB*
AEAD modes or
NewCTR
Go 1.24
golang.org/x/crypto/sha3
crypto/sha3
Go 1.24
golang.org/x/crypto/hkdf
crypto/hkdf
Go 1.24
golang.org/x/crypto/pbkdf2
crypto/pbkdf2
Go 1.24
testing/synctest.Run
testing/synctest.Test
Go 1.25
crypto.EncryptPKCS1v15
OAEP encryptionGo 1.26
net/http/httputil.ReverseProxy.Director
ReverseProxy.Rewrite
Go 1.26
废弃包/特性替代方案起始版本
math/rand
math/rand/v2
Go 1.22
crypto/elliptic
(多数函数)
crypto/ecdh
Go 1.21
reflect.SliceHeader
,
StringHeader
unsafe.Slice
,
unsafe.String
Go 1.21
reflect.PtrTo
reflect.PointerTo
Go 1.22
runtime.GOROOT()
go env GOROOT
Go 1.24
runtime.SetFinalizer
runtime.AddCleanup
Go 1.24
crypto/cipher.NewOFB
,
NewCFB*
AEAD模式或
NewCTR
Go 1.24
golang.org/x/crypto/sha3
crypto/sha3
Go 1.24
golang.org/x/crypto/hkdf
crypto/hkdf
Go 1.24
golang.org/x/crypto/pbkdf2
crypto/pbkdf2
Go 1.24
testing/synctest.Run
testing/synctest.Test
Go 1.25
crypto.EncryptPKCS1v15
OAEP加密Go 1.26
net/http/httputil.ReverseProxy.Director
ReverseProxy.Rewrite
Go 1.26

Migration Priority Guide

迁移优先级指南

When modernizing a codebase, prioritize changes by impact:
在对代码库进行现代化改造时,请根据影响程度优先处理以下内容:

High priority (safety and correctness)

高优先级(安全性与正确性)

  1. Remove loop variable shadow copies (Go 1.22+) — prevents subtle bugs
  2. Replace
    math/rand
    with
    math/rand/v2
    (Go 1.22+) — remove
    rand.Seed
    calls
  3. Use
    os.Root
    for user-supplied file paths (Go 1.24+) — prevents path traversal
  4. Run
    govulncheck
    (Go 1.22+) — catch known vulnerabilities
  5. Use
    errors.Is
    /
    errors.As
    instead of direct comparison (Go 1.13+)
  6. Migrate deprecated crypto packages (Go 1.24+) — security critical
  1. 移除循环变量的影子副本 (Go 1.22+) —— 防止潜在的隐蔽bug
  2. math/rand/v2
    替代
    math/rand
    (Go 1.22+) —— 移除
    rand.Seed
    调用
  3. 对用户提供的文件路径使用
    os.Root
    (Go 1.24+) —— 防止路径遍历攻击
  4. 运行
    govulncheck
    (Go 1.22+) —— 检测已知漏洞
  5. errors.Is
    /
    errors.As
    替代直接比较 (Go 1.13+)
  6. 迁移废弃的加密包 (Go 1.24+) —— 安全至关重要

Medium priority (readability and maintainability)

中优先级(可读性与可维护性)

  1. Replace
    interface{}
    with
    any
    (Go 1.18+)
  2. Use
    min
    /
    max
    builtins (Go 1.21+)
  3. Use
    range
    over int (Go 1.22+)
  4. Use
    slices
    and
    maps
    packages (Go 1.21+)
  5. Use
    cmp.Or
    for default values (Go 1.22+)
  6. Use
    sync.OnceValue
    /
    sync.OnceFunc
    (Go 1.21+)
  7. Use
    sync.WaitGroup.Go
    (Go 1.25+)
  8. Use
    t.Context()
    in tests (Go 1.24+)
  9. Use
    b.Loop()
    in benchmarks (Go 1.24+)
  1. any
    替代
    interface{}
    (Go 1.18+)
  2. 使用
    min
    /
    max
    内置函数 (Go 1.21+)
  3. 使用整数范围遍历
    range
    (Go 1.22+)
  4. 使用
    slices
    maps
    (Go 1.21+)
  5. cmp.Or
    设置默认值 (Go 1.22+)
  6. 使用
    sync.OnceValue
    /
    sync.OnceFunc
    (Go 1.21+)
  7. 使用
    sync.WaitGroup.Go
    (Go 1.25+)
  8. 在测试中使用
    t.Context()
    (Go 1.24+)
  9. 在基准测试中使用
    b.Loop()
    (Go 1.24+)

Lower priority (gradual improvement)

低优先级(逐步优化)

  1. Migrate to
    slog
    from third-party loggers (Go 1.21+)
  2. Adopt iterators where they simplify code (Go 1.23+)
  3. Replace
    sort.Slice
    with
    slices.SortFunc
    (Go 1.21+)
  4. Use
    strings.SplitSeq
    and iterator variants (Go 1.24+)
  5. Move tool deps to
    go.mod
    tool directives (Go 1.24+)
  6. Enable PGO for production builds (Go 1.21+)
  7. Upgrade to golangci-lint v2 with modernize linter (golangci-lint v2.6.0+)
  8. Add
    govulncheck
    to CI pipeline
  9. Set up monthly modernization CI pipeline
  10. Evaluate
    encoding/json/v2
    for new code (Go 1.25+, experimental)
  1. 从第三方日志库迁移到
    slog
    (Go 1.21+)
  2. 在可简化代码的场景下使用迭代器 (Go 1.23+)
  3. slices.SortFunc
    替代
    sort.Slice
    (Go 1.21+)
  4. 使用
    strings.SplitSeq
    及其迭代器变体 (Go 1.24+)
  5. 将工具依赖移至
    go.mod
    的tool指令中 (Go 1.24+)
  6. 在生产构建中启用PGO (Go 1.21+)
  7. 升级到golangci-lint v2并启用modernize检查器 (golangci-lint v2.6.0+)
  8. 在CI流水线中添加
    govulncheck
  9. 建立月度现代化CI流水线
  10. 为新代码评估
    encoding/json/v2
    (Go 1.25+,实验性)

Related Skills

相关技能

See
samber/cc-skills-golang@golang-concurrency
,
samber/cc-skills-golang@golang-testing
,
samber/cc-skills-golang@golang-observability
,
samber/cc-skills-golang@golang-error-handling
,
samber/cc-skills-golang@golang-linter
skills.
请查看
samber/cc-skills-golang@golang-concurrency
samber/cc-skills-golang@golang-testing
samber/cc-skills-golang@golang-observability
samber/cc-skills-golang@golang-error-handling
samber/cc-skills-golang@golang-linter
技能。