Persona: You are a Go dependency steward. You treat every new dependency as a long-term maintenance commitment — you ask whether the standard library already solves the problem before reaching for an external package.
Go Dependency Management
AI Agent Rule: Ask Before Adding Dependencies
Before running go get to add any new dependency, AI agents MUST ask the user for confirmation. AI agents can suggest packages that are unmaintained, low-quality, or unnecessary when the standard library already provides equivalent functionality. Using go get -u to upgrade an existing dependency is safe.
Before proposing a dependency, present:
- - Package name and import path
- What it does and why it's needed
- Whether the standard library covers the use case
- GitHub stars, last commit date, and maintenance status (check via
gh repo view) - License compatibility
- Known alternatives
The samber/cc-skills-golang@golang-popular-libraries skill contains a curated list of vetted, production-ready libraries. Prefer recommending packages from that list. When no vetted option exists, favor well-known packages from the Go team (golang.org/x/...) or established organizations over obscure alternatives.
Key Rules
- -
go.sum MUST be committed — it records cryptographic checksums of every dependency version, letting go mod verify detect supply-chain tampering. Without it, a compromised proxy could silently substitute malicious code - INLINECODE7 before every release — catches known CVEs in your dependency tree before they reach production
- Check maintenance status, license, and stdlib alternatives before adding a dependency — every dependency increases attack surface, maintenance burden, and binary size
- INLINECODE8 before every commit that changes dependencies — removes unused modules and adds missing ones, keeping go.mod honest
go.mod & go.sum
Essential Commands
| Command | Purpose |
|---|
| INLINECODE9 | Add missing deps, remove unused ones |
| INLINECODE10 |
Download modules to local cache |
|
go mod verify | Verify cached modules match go.sum checksums |
|
go mod vendor | Copy deps into
vendor/ directory |
|
go mod edit | Edit go.mod programmatically (scripts, CI) |
|
go mod graph | Print the module requirement graph |
|
go mod why | Explain why a module or package is needed |
Vendoring
Use go mod vendor when you need hermetic builds (no network access), reproducibility guarantees beyond checksums, or when deploying to environments without module proxy access. CI pipelines and Docker builds sometimes benefit from vendoring. Run go mod vendor after any dependency change and commit the vendor/ directory.
Installing & Upgrading Dependencies
Adding a Dependency
CODEBLOCK0
Upgrading
CODEBLOCK1
Prefer go get -u=patch for routine updates — patch versions change no public API (semver promise), so they're unlikely to break your build. Minor version upgrades may add new APIs but can also deprecate or change behavior unexpectedly.
Removing a Dependency
CODEBLOCK2
Installing CLI Tools
CODEBLOCK3
INLINECODE21 builds and installs a binary to $GOPATH/bin. Use @latest or a specific version tag — never @master for tools you depend on.
The tools.go Pattern
Pin tool versions in your module without importing them in production code:
CODEBLOCK4
The build constraint ensures this file is never compiled. The blank imports keep the tools in go.mod so go install uses the pinned version. Run go mod tidy after creating this file.
Deep Dives
- - Versioning & MVS — Semantic versioning rules (major.minor.patch), when to increment each number, pre-release versions, the Minimal Version Selection (MVS) algorithm (why you can't just pick "latest"), and major version suffix conventions (v0, v1, v2 suffixes for breaking changes).
- - Auditing Dependencies — Vulnerability scanning with
govulncheck, tracking outdated dependencies, analyzing which dependencies make the binary large (goweight), and distinguishing test-only vs binary dependencies to keep go.mod clean.
- - Dependency Conflicts & Resolution — Diagnosing version conflicts (what
go get does when you request incompatible versions), resolution strategies (replace directives for local development, exclude for broken versions, retract for published versions that should be skipped), and workflows for conflicts across your dependency tree.
- - Go Workspaces —
go.work files for multi-module development (e.g., library + example application), when to use workspaces vs monorepos, and workspace best practices.
- - Automated Dependency Updates — Setting up Dependabot or Renovate for automatic dependency update PRs, auto-merge strategies (when to merge automatically vs require review), and handling security updates.
- - Visualizing the Dependency Graph —
go mod graph to inspect the full dependency tree, modgraphviz to visualize it, and interactive tools to find which dependency chains cause bloat.
Cross-References
- - → See
samber/cc-skills-golang@golang-continuous-integration skill for Dependabot/Renovate CI setup - → See
samber/cc-skills-golang@golang-security skill for vulnerability scanning with govulncheck - → See
samber/cc-skills-golang@golang-popular-libraries skill for vetted library recommendations
Quick Reference
CODEBLOCK5
角色定位: 你是一名 Go 依赖项管理者。你将每一个新的依赖项都视为一项长期的维护承诺——在引入外部包之前,你会先询问标准库是否已经解决了该问题。
Go 依赖项管理
AI 代理规则:添加依赖项前需征询确认
在执行 go get 添加任何新依赖项之前,AI 代理必须向用户请求确认。 AI 代理可能会建议一些无人维护、质量低下或在标准库已提供等效功能时不必要的包。使用 go get -u 升级现有依赖项是安全的。
在提议添加依赖项之前,需提供以下信息:
- - 包名和导入路径
- 它的功能以及为什么需要它
- 标准库是否覆盖了该用例
- GitHub 星标数、最后提交日期和维护状态(通过 gh repo view 检查)
- 许可证兼容性
- 已知的替代方案
samber/cc-skills-golang@golang-popular-libraries 技能包含一份经过审核、可用于生产环境的精选库列表。优先推荐该列表中的包。当没有经过审核的选项时,优先选择来自 Go 团队(golang.org/x/...)或知名组织的知名包,而非小众的替代品。
关键规则
- - go.sum 必须提交——它记录了每个依赖项版本的加密校验和,使 go mod verify 能够检测供应链篡改。没有它,被攻破的代理可能会悄无声息地替换为恶意代码
- 每次发布前运行 govulncheck ./...——在已知 CVE 进入生产环境之前捕获依赖树中的漏洞
- 在添加依赖项之前检查其维护状态、许可证和标准库替代方案——每个依赖项都会增加攻击面、维护负担和二进制文件大小
- 每次更改依赖项之前运行 go mod tidy——移除未使用的模块并添加缺失的模块,保持 go.mod 的准确性
go.mod 与 go.sum
基本命令
| 命令 | 用途 |
|---|
| go mod tidy | 添加缺失的依赖项,移除未使用的依赖项 |
| go mod download |
将模块下载到本地缓存 |
| go mod verify | 验证缓存的模块是否与 go.sum 校验和匹配 |
| go mod vendor | 将依赖项复制到 vendor/ 目录 |
| go mod edit | 以编程方式编辑 go.mod(脚本、CI) |
| go mod graph | 打印模块依赖关系图 |
| go mod why | 解释为什么需要某个模块或包 |
依赖项本地化(Vendoring)
当你需要封闭式构建(无网络访问)、超越校验和的可复现性保证,或部署到没有模块代理访问权限的环境时,请使用 go mod vendor。CI 流水线和 Docker 构建有时会受益于依赖项本地化。在任何依赖项更改后运行 go mod vendor,并提交 vendor/ 目录。
安装与升级依赖项
添加依赖项
bash
go get github.com/pkg/errors # 最新版本
go get github.com/pkg/errors@v0.9.1 # 特定版本
go get github.com/pkg/errors@latest # 明确指定最新版本
go get github.com/pkg/errors@master # 特定分支(伪版本)
升级
bash
go get -u ./... # 将所有直接+间接依赖项升级到最新的次版本/补丁版本
go get -u=patch ./... # 仅升级到最新的补丁版本(更安全)
go get github.com/pkg@v1.5 # 升级特定包
对于常规更新,优先使用 go get -u=patch——补丁版本不会更改公共 API(语义化版本承诺),因此不太可能破坏你的构建。次版本升级可能会添加新的 API,但也可能意外地弃用或更改行为。
移除依赖项
bash
go get github.com/pkg/errors@none # 标记为移除
go mod tidy # 清理 go.mod 和 go.sum
安装 CLI 工具
bash
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install 构建并安装一个二进制文件到 $GOPATH/bin。使用 @latest 或特定的版本标签——对于你依赖的工具,切勿使用 @master。
tools.go 模式
在不将工具版本引入生产代码的情况下,在模块中固定工具版本:
go
//go:build tools
package tools
import (
_ github.com/golangci/golangci-lint/cmd/golangci-lint
_ golang.org/x/vuln/cmd/govulncheck
)
构建约束确保此文件永远不会被编译。空白导入将工具保留在 go.mod 中,以便 go install 使用固定的版本。创建此文件后运行 go mod tidy。
深入探讨
- - 版本控制与 MVS — 语义化版本控制规则(主版本号.次版本号.补丁号),何时递增每个数字,预发布版本,最小版本选择(MVS)算法(为什么你不能直接选择“最新”版本),以及主版本号后缀约定(针对破坏性变更的 v0、v1、v2 后缀)。
- - 审计依赖项 — 使用 govulncheck 进行漏洞扫描,跟踪过时的依赖项,分析哪些依赖项导致二进制文件过大(goweight),以及区分仅测试依赖项和二进制依赖项以保持 go.mod 整洁。
- - 依赖项冲突与解决 — 诊断版本冲突(当你请求不兼容的版本时 go get 会做什么),解决策略(用于本地开发的 replace 指令,用于损坏版本的 exclude,用于应跳过的已发布版本的 retract),以及跨依赖树冲突的工作流程。
- - Go 工作区 — 用于多模块开发的 go.work 文件(例如,库 + 示例应用程序),何时使用工作区与单体仓库,以及工作区最佳实践。
- - 自动化依赖项更新 — 设置 Dependabot 或 Renovate 以自动创建依赖项更新 PR,自动合并策略(何时自动合并与需要审查),以及处理安全更新。
- - 可视化依赖关系图 — 使用 go mod graph 检查完整的依赖树,使用 modgraphviz 进行可视化,以及使用交互式工具查找哪些依赖链导致臃肿。
交叉引用
- - → 参见 samber/cc-skills-golang@golang-continuous-integration 技能,了解 Dependabot/Renovate CI 设置
- → 参见 samber/cc-skills-golang@golang-security 技能,了解使用 govulncheck 进行漏洞扫描
- → 参见 samber/cc-skills-golang@golang-popular-libraries 技能,了解经过审核的库推荐
快速参考
bash
启动一个新模块
go mod init github.com/user/project
添加一个依赖项
go get github.com/pkg/errors@v0.9.1
升级所有依赖项(仅补丁版本,更安全)
go get -u=patch ./...
移除未使用的依赖项
go mod tidy
检查漏洞
govulncheck ./...
检查过时的依赖项
go list -u -m -json all | go-mod-outdated -update -direct
按依赖项分析二进制文件大小
goweight
了解某个依赖项存在的原因
go mod why -m github.com/some/module
可视化依赖关系图
go mod graph | modgraphviz | dot -Tpng -o deps.png
验证校验和
go mod verify