基本使用

命令说明
init初始化go mod,命令:go mod init xxx(package name)
downloaddownload modules to local cache(全局下载依赖包)
editedit go.mod from tools or scripts(编辑go.mod)
graphprint module requirement graph (打印模块依赖图)
verifyinitialize new module in current directory(在当前目录初始化mod)
tidyadd missing and remove unused modules(拉取缺少的模块,移除不用的模块)
vendormake vendored copy of dependencies(将依赖复制到vendor下)
verifyverify dependencies have expected content (验证依赖是否正确)
whyexplain why packages or modules are needed(解释为什么需要依赖)

具体使用案例

# 1, Go mod 初始化
go mod init 模块名

# 2, Go mod 下载到本地Cache
go mod download
# 2, Go mod 清理本地Cache
go clean -modcache

# 3, Go mod 编辑go.mod文件:更多go mod查看 `go help mod edit`
go mod edit

# 4, Go mod 打印依赖图
go mod graph

# 5, Go mod 删除错误或者不使用的modules
go mod tidy

# 6, Go mod 生成vendor目录
go mod vendor

# 7, Go mod 验证依赖是否正确
go mod verify

# 8, Go mod 查找依赖
go mod why

# 9, GO mod 更新依赖到最新版本
go get -u github.com/golang/protobuf

# 10, Go mod 更新到指定版本
go get -u github.com/golang/protobuf@指定版本
# 10, Go mod 查看有哪些版本
go list -m -versions github.com/golang/protobuf

# 11, Go mod 替换包源
go mod edit -replace=golang.org/x/crypto@v0.0.0=github.com/golang/crypto@latest
go mod edit -replace=golang.org/x/sys@v0.0.0=github.com/golang/sys@latest

# 12, Go打包给其他包调用
git tag -a v0.0.1 -m "Golang打包给其他包调用" && git push origin v0.0.1
go get -u xxxxxx

本段参考:https://blog.csdn.net/m0_37698164/article/details/127548294