SKILL·E35BFE

go-modernize

eduardo-sl
更新于 8 days ago
64
9
64
在 GitHub 上查看
开发general

关于

This skill modernizes Go code by updating it to use newer language features from Go 1.21-1.23+, including generics, log/slog, errors.Join, and the slices/maps packages. Use it specifically when you want to refactor legacy patterns like `interface{}` or adopt features such as range-over-func and iterators. It is not for general style, error philosophy, or logging architecture—use other dedicated skills for those.

快速安装

Claude Code

推荐
主要方式
npx skills add eduardo-sl/go-agent-skills -a claude-code
插件命令备选方式
/plugin add https://github.com/eduardo-sl/go-agent-skills
Git 克隆备选方式
git clone https://github.com/eduardo-sl/go-agent-skills.git ~/.claude/skills/go-modernize

在 Claude Code 中复制并粘贴此命令以安装该技能

技能文档

Go Modernize

Go evolves. Code written for Go 1.16 should not look the same as code targeting Go 1.22+. Modernize incrementally — update go.mod, then adopt new patterns.

Detailed reference material, loaded on demand:

  • references/generics.md — replacing interface{} with type parameters, constraints, generic containers, when NOT to use generics.
  • references/stdlib-migrations.md — before/after examples for slog, errors.Join, slices/maps helpers, range-over-int, and iterators.

Read a reference file only when the summary below is not enough.

Modernization Procedure

  1. Check the go directive in go.mod — it caps which features you can use.

  2. Run the official modernize analyzer first — it finds and fixes the mechanical migrations automatically:

    go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...
    

    If the command is unavailable in your environment, apply the table below manually instead.

  3. Scan the table below for the judgment-based migrations the analyzer does not cover (generics, iterators, logger replacement) and apply them case by case.

  4. Run go build ./... and the test suite after each group of changes.

Feature Table by Go Version

Go VersionFeatureAction
1.13+errors.Is, errors.AsReplace == error comparisons
1.13+http.NewRequestWithContextReplace http.NewRequest
1.16+embedReplace go-bindata / packr
1.18+GenericsReplace interface{} utility functions
1.20+errors.JoinReplace manual error accumulation
1.21+log/slogReplace log for structured logging
1.21+slices, mapsReplace hand-written slice/map utilities
1.21+min, max builtinsReplace math.Min/math.Max (float64-only)
1.22+Range over intReplace for i := 0; i < n; i++
1.23+Range over funcReplace callback-based iteration

Key Migrations at a Glance

Generics — type-safe utilities (Go 1.18+)

// ❌ Before — loses type safety
func Contains(slice []interface{}, target interface{}) bool { /* ... */ }

// ✅ After — type-safe generic
func Contains[T comparable](slice []T, target T) bool { /* ... */ }

Use generics for container types (Set[T], Result[T]) and utility functions. Do NOT use them where a single concrete type works, or as a substitute for interfaces in runtime polymorphism. Details and constraint patterns: references/generics.md.

Structured logging (Go 1.21+)

// ❌ Before
log.Printf("processing order %s for user %s", orderID, userID)

// ✅ After
slog.Info("processing order",
    slog.String("order_id", orderID),
    slog.String("user_id", userID),
)

Keep zap/zerolog only if you need their performance for high-throughput logging; for most services slog is sufficient.

errors.Join (Go 1.20+)

var errs []error
for _, item := range items {
    if err := validate(item); err != nil {
        errs = append(errs, err)
    }
}
if err := errors.Join(errs...); err != nil {
    return fmt.Errorf("validation: %w", err)
}

errors.Join preserves the chain — errors.Is/errors.As work on each joined error. Never accumulate error strings manually.

slices and maps helpers (Go 1.21+)

found := slices.Contains(items, target)          // not a manual loop
slices.SortFunc(users, func(a, b User) int {     // not sort.Slice
    return cmp.Compare(a.Name, b.Name)
})
keys := slices.Collect(maps.Keys(m))             // not a manual key loop
clone := maps.Clone(m)                           // not a manual copy loop

Range over int (Go 1.22+) and iterators (Go 1.23+)

for i := range n { process(i) }                  // not for i := 0; i < n; i++

for i, v := range slices.Backward(items) {       // stdlib iterators
    fmt.Printf("%d: %v\n", i, v)
}

Custom iter.Seq/iter.Seq2 iterators replace callback-based iteration — full worked example in references/stdlib-migrations.md.

Context-aware HTTP requests (Go 1.13+, often missed)

// ❌ Before — request without context
req, err := http.NewRequest(http.MethodGet, url, nil)

// ✅ After — context propagated
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)

Verification Checklist

  1. go.mod version matches the features used in the codebase
  2. No interface{} where any or type parameters would be clearer
  3. log/slog used instead of log.Printf for structured logging
  4. errors.Join used instead of manual error string concatenation
  5. slices.Contains, slices.SortFunc, maps.Clone replace hand-written loops
  6. Range over int (for i := range n) used where applicable
  7. http.NewRequestWithContext used instead of http.NewRequest
  8. No sort.Slice — use slices.SortFunc with cmp.Compare
  9. Generics used for type-safe containers and utilities, not overused for trivial cases
  10. Third-party dependencies evaluated against stdlib alternatives added in recent Go versions

GitHub 仓库

eduardo-sl/go-agent-skills
路径: skills/(code-quality)/go-modernize
0
FAQ

常见问题

什么是 go-modernize Skill?

go-modernize 是一个 Claude Skill,作者为 eduardo-sl。Skill 将 Claude 按需加载的说明和资源打包,让 Claude 无需额外提示即可执行与 go-modernize 相关的任务。

如何安装 go-modernize?

使用本页的安装命令:将 go-modernize 作为插件添加到 Claude Code,或将其仓库克隆到 skills 目录,然后重启 Claude 以加载该 Skill。

go-modernize 属于哪个分类?

go-modernize 属于开发分类。

go-modernize 可以免费使用吗?

可以。go-modernize 已收录在 AIMCP,可免费安装。

相关推荐技能

qmd
开发

这是一个本地搜索和索引的CLI工具,支持BM25、向量搜索和重排序功能。开发者可以用它快速索引本地文件(如Markdown文档)并进行混合搜索,特别适合代码库或文档的本地检索。它还提供MCP模式,能轻松集成到Claude开发环境中使用。

查看技能
subagent-driven-development
开发

该Skill用于在当前会话中执行包含独立任务的实施计划,它会为每个任务分派一个全新的子代理并在任务间进行代码审查。这种"全新子代理+任务间审查"的模式既能保障代码质量,又能实现快速迭代。适合需要在当前会话中连续执行独立任务,并希望在每个任务后都有质量把关的开发场景。

查看技能
mcporter
开发

mcporter Skill 让开发者能在Claude中直接管理和调用MCP服务器。它支持列出可用服务器、调用工具、处理OAuth认证以及管理服务器守护进程。开发者可以通过命令行式交互快速执行`mcporter list`查看服务器,或使用`mcporter call`直接调用工具,简化了MCP工作流程。

查看技能
adk-deployment-specialist
开发

这是一个用于部署和编排Google Vertex AI ADK智能体的Claude Skill,专为构建生产级多智能体系统而设计。它支持通过A2A协议进行智能体通信,提供代码执行沙箱和记忆库功能,并能处理智能体发现与任务提交。当开发者需要部署ADK智能体或编排多智能体协作时,可使用此Skill来简化Vertex AI Agent Engine的部署流程。

查看技能