添加外部依赖
go get、版本管理与模块缓存
添加外部依赖 是 CoddyKit 上的免费 Go Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Go Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Go Academy 课程共包含 4 节课。
查找包
查找 Go 包的主要资源是 pkg.go.dev。您可以搜索包、阅读文档并查看使用示例:
# Search and explore at https://pkg.go.dev
# Popular packages:
# github.com/gin-gonic/gin — HTTP framework
# github.com/stretchr/testify — test assertions
# golang.org/x/sync — extended sync primitives
# github.com/spf13/cobra — CLI framework
# github.com/jackc/pgx/v5 — PostgreSQL drivergo get:添加依赖项
使用 go get 添加依赖项。它会下载模块并更新 go.mod 和 go.sum:
# Add latest version
go get github.com/stretchr/testify
# Add specific version
go get github.com/stretchr/testify@v1.8.4
# Add a pre-release version
go get github.com/foo/bar@main
# go get updates go.mod:
# require github.com/stretchr/testify v1.8.4使用依赖项
运行 go get 后,按常规方式导入并使用该包:
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestAdd(t *testing.T) {
assert.Equal(t, 4, 2+2, "math should work")
fmt.Println("assertion done")
_ = assert.New // suppress unused import
}go mod download
预先将 go.mod 中列出的所有依赖项下载到模块缓存:
# Download all deps from go.mod
go mod download
# Download specific module
go mod download github.com/gin-gonic/gin@v1.9.1
# Useful for:
# - Docker layer caching (copy go.mod/go.sum first, then download)
# - Pre-warming CI cache
# - Offline development setup语义化版本选择:MVS
Go 使用最低版本选择(MVS):它始终选择满足所有依赖项要求的最低版本。不会自动升级主版本:
# If A requires gin v1.9.0
# and B requires gin v1.9.1
# MVS selects v1.9.1 (minimum that satisfies both)
# To upgrade ALL dependencies to latest minor/patch:
go get -u ./...
# To upgrade specific package:
go get -u github.com/gin-gonic/gingo mod why——解释依赖关系
找出构建中需要某个模块的原因:
# Why is encoding/json needed?
go mod why encoding/json
# Why is golang.org/x/net needed?
go mod why golang.org/x/net
# Output shows the import chain:
# github.com/myapp
# github.com/gin-gonic/gin
# golang.org/x/net使用 go mod vendor 进行可复现构建
供应商模式会将所有依赖项锁定在您的代码库中,从而实现可复现构建:
# Vendor all deps
go mod vendor
# Build only from vendor (no network)
go build -mod=vendor ./...
# Dockerfile pattern:
# COPY go.mod go.sum ./
# RUN go mod download
# COPY . .
# RUN go build ...使用 GONOSUMCHECK 和 GOPRIVATE 管理私有模块
配置 Go,以使用托管在内部服务器上的私有模块:
# Tell Go not to use proxy/sumdb for private modules
export GOPRIVATE=github.com/mycompany/*
export GONOSUMCHECK=github.com/mycompany/*
# Or in go env:
go env -w GOPRIVATE=github.com/mycompany/*
# For enterprise Go proxy:
export GOPROXY=https://proxy.mycompany.com,directgo clean -modcache
清除模块缓存,以强制重新下载所有依赖项:
# Clear entire module cache
go clean -modcache
# Clear cache for specific version
go clean -modcache github.com/gin-gonic/gin@v1.9.1
# Useful when:
# - Cache is corrupted
# - Testing with a fresh environment
# - Disk space cleanup检查漏洞:govulncheck
Go 官方漏洞扫描器会检查您的依赖项中是否存在已知的 CVE 漏洞:
# Install govulncheck
go install golang.org/x/vuln/cmd/govulncheck@latest
# Scan your module
govulncheck ./...
# Output example:
# Vulnerability #1: GO-2023-1234
# github.com/foo/bar v1.2.3
# Fixed in: v1.2.4
# Suggestion: go get github.com/foo/bar@v1.2.4快速检查
哪个命令可以将依赖项的指定版本添加到模块中?
回顾:外部依赖项
总结:
go get pkg@version添加依赖项- Go 使用 MVS——满足所有要求的最低版本
go mod tidy清理未使用的依赖项go mod vendor用于完全可复现的离线构建- 私有模块请使用
GOPRIVATE - 运行
govulncheck查找漏洞
常见问题解答
「添加外部依赖」课时是免费的吗?
是的 — 「添加外部依赖」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Go Academy 课程的其余内容,请升级到 CoddyKit PRO。 Go Academy 课程共包含 4 节课。
「添加外部依赖」这节课中我会学到什么?
go get、版本管理与模块缓存 你通过在浏览器中直接运行的动手代码来练习 Go Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Go Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Go Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「添加外部依赖」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Go Academy 课中编写并运行代码吗?
能。每节 Go Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。