Agent skill
code-refactor
Use when refactoring Go code for better structure, maintainability, extensibility, or testability - follows SOLID principles and idiomatic Go patterns
Install this agent skill to your Project
npx add-skill https://github.com/unix2dos/skills/tree/main/code-refactor
SKILL.md
Code Refactor
Golang 代码重构技能,帮助重构和优化 Go 代码。
When to Use
- 需要改变代码结构(拆函数、拆模块、改接口)
- 需要引入设计模式(依赖注入、函数选项模式等)
- 改善可扩展性、可测试性
- 涉及 SOLID 原则级别的结构调整
When NOT to Use
- 只需要删代码、降复杂度、去冗余 → 用 code-simplifier
- 需要审查安全/并发/性能问题 → 用 go-code-review
- 非 Go 语言代码 → 用 code-simplifier(支持任何语言)
重构约束(按优先级执行)
P0 - 行为等价性(不可妥协)
- 所有公开 API 的输入输出保持一致
- 边界条件处理不变
- 并发安全性保留
- 如发现原代码存在逻辑缺陷,明确标注但不修复
P1 - 可维护性
目标:降低认知负担
- 函数职责单一(单一职责原则)
- 命名自解释(避免注释依赖)
- 嵌套层级 ≤ 3
- 圈复杂度 ≤ 10(特殊业务逻辑可放宽至 15)
- 使用 Early Return 减少嵌套
P2 - 可扩展性
策略:接口隔离 + 依赖注入
- 反过度设计:不为"可能"的需求预留扩展点
- 使用函数选项模式(Functional Options)
- 配置与逻辑分离
P3 - 可测试性
原则:依赖可替换
- 外部依赖通过接口注入
- 避免包级变量(logger 等除外)
- 纯函数优先
输出格式
先输出以下诊断报告,等待用户确认后,再进行重构。
重构策略
- 核心改动:[用一句话说明主要重构方向]
- 设计模式:[使用的模式及理由]
- 权衡说明:[哪些地方优先实用性而非理论完美]
风险评估
- 破坏性变更:[是/否],具体:xxx
- 性能影响:[提升/下降/持平],原因:xxx
- 依赖变化:[新增/移除的依赖]
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
history-autopsy
Only invoke when explicitly requested via "历史速览"、"@history-autopsy" or "history autopsy". Do NOT auto-trigger.
learn-tech
Only invoke when explicitly requested via "学习"、"讲解"、"teach me"、"@learn-tech". Do NOT auto-trigger.
go-code-review
Use when reviewing Go code for performance, concurrency safety, security vulnerabilities, or readability issues
wisdom-decoder
Only invoke when explicitly requested via "智慧解码"、"@wisdom-decoder" or "wisdom decoder". Do NOT auto-trigger.
insight-miner
Only invoke when explicitly requested via "洞见"、"@insight-miner" or "insight". Do NOT auto-trigger.
confidence-check
Use when starting complex tasks like feature development, bug fixes, or code refactoring - runs a pre-implementation confidence check to avoid wasting tokens on wrong directions
Didn't find tool you were looking for?