golang vendor包管理和go mod

本文介绍了Go语言1.11版本引入的go mod模块管理系统,用于管理项目中第三方包的依赖。通过设置GO111MODULE环境变量来启用或禁用go mod。同时,文章也提及了vendor包管理工具govendor的使用,包括安装、初始化和添加包的基本操作。go mod和govendor为Go项目的包管理提供了便捷的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

go mod

golang1.11版本添加了go mod来管理项目引用的第三方的包,并且可以和vendor互相切换。
保证go version在1.11以上。你的项目是不能直接在$GOPATH/src下的,但是可以在$GOPATH/src下再建立一个文件来作为项目根目录,或者在其他非$GOPATH/src的目录建立项目。我选择在$GOPATH/src/p1-dhh来作为项目根目录。
想要使用先学习一下go mod的用法。

  1. go mod启用,项目目录非$GOPATH/src,用 export GO111MODULE=on|off|auto来控制go mod的开启。
    on:开启。off:关闭。auto:自动(如果是在$GOPATH/src下,那么直接用$GOPATH/src
  2. go mod命令行参数:
go mod <command> [arguments]含义
downloaddownload modules to local cache(下载模块到本地cache目录下)
editedit go.mod from tools or scripts(编辑go.mod文件)
graphprint module requirement graph(打印出模块的需求图)
initinitialize new module in current directory(初始化当前目录的新模块)
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;添加依赖 go mod tidy
  2. go mod 替换golang.org/x
module p1-dhh
go 1.12
replace (
	golang.org/x/net => github.com/golang/net latest
	golang.org/x/sync => github.com/golang/sync latest
	golang.org/x/sys => github.com/golang/sys latest
)

golang.org/x的包都有对应的github的镜像,不知道版本不要紧,统一用latest最新,然后命令行 go mod tidy,就会更新文件了。

vendor

地址:github.com/kardianos/govendor
安装好以后,试试命令行govendor,如果找不到命令,那就把govendor的可执行文件放到该放的地方,或者配置好环境变量。

  1. govendor命令参数:
govendor <command>含义
initCreate the “vendor” folder and the “vendor.json” file.(创建vendor/vendor.json到当前目录)
listList and filter existing dependencies and packages.(列出依赖包)
addAdd packages from $GOPATH.(从$GOPATH添加依赖包)
updateUpdate packages from $GOPATH. (更新依赖包)
removeRemove packages from the vendor folder.(移除依赖包)
statusLists any packages missing, out-of-date, or modified locally.(列出任何丢失、过期或本地修改的包)
fetchAdd new or update vendor folder packages from remote repository.(从远程存储库添加新的或更新vendor文件的包)
syncPull packages into vendor folder from remote repository with revisions from vendor.json file.(将包从远程存储库拖到vendor文件夹,并修改json文件)
migrateMove packages from a legacy tool to the vendor folder with metadata.(将包从遗留工具移动到包含元数据的vendor文件夹)
getLike “go get” but copies dependencies into a “vendor” folder.
licenseList discovered licenses for the given status or import paths.
shellRun a “shell” to make multiple sub-commands more efficient for large projects.
  1. govendor 补充参数:
+local(l) packages in your project
+external(e) referenced packages in GOPATH but not in current project
+vendor(v) packages in the vendor folder
+std(s) packages in the standard library
+excluded(x) external packages explicitly excluded from vendoring
+unused(u) packages in the vendor folder, but unused
+missing(m) referenced packages but not found
+program§ package is a main package
+outside+external +missing
+all+all packages
  1. 初始化 govendor init
  2. 添加包 govendor add xxxx

之后如果有新的体会会继续更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值