关于
This Claude Skill provides a safe, behavior-preserving refactoring workflow for Go codebases, ensuring all changes are compiler-checked and backed by passing tests. It handles tasks like renaming across the codebase, extracting packages, breaking circular dependencies, and managing strangler migrations. Use it for structural cleanups without changing functionality, but not for architectural decisions, adopting new language features, or performance rewrites.
快速安装
Claude Code
推荐npx skills add eduardo-sl/go-agent-skills -a claude-code/plugin add https://github.com/eduardo-sl/go-agent-skillsgit clone https://github.com/eduardo-sl/go-agent-skills.git ~/.claude/skills/go-refactoring在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
Go Refactoring Workflow
A refactor changes structure, never behavior. The definition of done is mechanical: same tests pass before and after every step, and every step is small enough to revert alone.
1. The Loop
- Baseline:
go build ./... && go test ./...must be green before touching anything. If tests are missing around the target code, write characterization tests FIRST — they pin current behavior, even if that behavior looks wrong. - One transformation from the catalog below.
- Verify: build + tests +
go vet ./.... - Commit. Never mix a refactor commit with a behavior change —
reviewers can skim a
refactor:commit; they must scrutinize a mixed one. - Repeat.
If step 3 fails and the fix isn't obvious in a minute, revert the step rather than debugging a half-applied transformation.
2. Renames — Let Tools Do Them
# Preferred: gopls (understands types, interfaces, embedding)
gopls rename -w internal/service/user.go:#offset newName
# Module path or package import path changes:
# update go.mod, then rewrite imports mechanically
find . -name '*.go' -exec sed -i 's|github.com/acme/old|github.com/acme/new|g' {} +
go build ./... # the compiler is the reviewer
Never rename an exported identifier of a published library without a
deprecation cycle: add the new name, mark the old one
// Deprecated: use NewName., delete in the next major version.
3. Extract Package
Moving code out of a god package, in compiler-checked steps:
- Create the new package; move one type and its methods (
goplsor cut/paste), leaving everything else. - In the old package, add type aliases so nothing breaks:
type User = user.User(aliases,=, not definitions). - Build. Migrate importers to the new path in batches; build each batch.
- Delete the aliases when no importer remains.
This keeps every commit green with an arbitrarily large caller base.
4. Break a Circular Dependency
Packages a → b and b → a won't compile; near-cycles show up as
god packages. Three escapes, in order of preference:
- Extract the shared core: both
aandbactually depend on a type — move it to a third packagecthat imports nothing. - Invert with an interface: if
storecalls back intoservice, define the callback interface INstore(consumer side) and letserviceimplement it. The arrow flips at compile time. - Merge: if two packages can't be described without each other, they were one package all along.
5. Change a Function Signature Safely
For exported functions with many callers:
// Step 1 — add the new form alongside the old
func (s *Service) ProcessCtx(ctx context.Context, id string) error { ... }
// Step 2 — old form delegates; mark deprecated
// Deprecated: use ProcessCtx.
func (s *Service) Process(id string) error {
return s.ProcessCtx(context.Background(), id)
}
// Step 3 — migrate callers batch by batch, building each batch
// Step 4 — delete the old form (same module) or keep until next major (library)
Inside a single module, prefer atomic signature changes when the compiler can find every caller for you: change it, then chase the build errors — that's the compiler enumerating your TODO list.
6. Strangler Migration for Subsystems
Replacing a subsystem (old store, legacy client) too big for one PR:
- Define the consumer-side interface the callers actually need.
- Make the OLD implementation satisfy it; wire callers to the interface.
- Build the new implementation behind the same interface; test both implementations against one shared conformance test suite.
- Switch the wiring in the composition root (one line, one commit, trivially revertible). Feature-flag it if risk warrants.
- Delete the old implementation in its own commit.
7. What Is NOT a Refactor
- "While I'm here" bug fixes — separate commit before or after.
- Reordering struct fields used with positional literals, changing exported error strings callers match on, changing JSON tags — these are behavior changes wearing refactor clothes.
- Rewrites without tests. If you can't pin behavior first, you're not refactoring; you're gambling.
Verification Checklist
- Baseline build + tests green before the first change
- Characterization tests added where coverage was missing
- Each commit is one transformation;
refactor:commits contain no behavior change - Renames done via gopls/compiler, not find-and-replace on identifiers
- Package extractions used type aliases to stay green mid-flight
- No new dependency cycles (
go build ./...proves it) - Public API changes follow the deprecation cycle
go vet ./...and the full test suite pass at every commit, not just the last- git log reads as a sequence of safe, revertible steps
GitHub 仓库
常见问题
什么是 go-refactoring Skill?
go-refactoring 是一个 Claude Skill,作者为 eduardo-sl。Skill 将 Claude 按需加载的说明和资源打包,让 Claude 无需额外提示即可执行与 go-refactoring 相关的任务。
如何安装 go-refactoring?
使用本页的安装命令:将 go-refactoring 作为插件添加到 Claude Code,或将其仓库克隆到 skills 目录,然后重启 Claude 以加载该 Skill。
go-refactoring 属于哪个分类?
go-refactoring 属于测试分类。
go-refactoring 可以免费使用吗?
可以。go-refactoring 已收录在 AIMCP,可免费安装。
相关推荐技能
该Skill通过60+个学术基准测试(如MMLU、GSM8K等)评估大语言模型质量,适用于模型对比、学术研究及训练进度追踪。它支持HuggingFace、vLLM和API接口,被EleutherAI等行业领先机构广泛采用。开发者可通过简单命令行快速对模型进行多任务批量评估。
这个Claude Skill提供了关于Cloudflare Cron Triggers的完整知识库,用于通过cron表达式定时执行Workers。它支持配置周期性任务、维护作业和自动化工作流,并能处理常见的cron触发错误。开发者可以用它来设置定时任务、测试cron处理器,并集成Workflows和Green Compute功能。
该Skill为开发者提供了基于Playwright的本地Web应用测试工具集,支持自动化测试前端功能、调试UI行为、捕获屏幕截图和查看浏览器日志。它包含管理服务器生命周期的辅助脚本,可直接作为黑盒工具运行而无需阅读源码。适用于需要快速验证本地Web应用界面和交互功能的开发场景。
这个Skill用于开发分支完成后的集成决策,当代码实现完成且测试通过时,它会引导开发者选择合适的工作流。它首先验证测试状态,然后提供合并、创建PR或清理等结构化选项。核心价值在于确保代码质量的同时,标准化分支收尾流程。
