很多语言有专门的工具或者库来管理项目中的依赖包或第三方库,比如java有maven
,javascript有npm
。但对于go 来说,v1.1
之前是没有内置的go模块工具的,项目文件需要放在GOPATH/src
下来运行,这样的工作方式比较强制,可能会降低效率。在v1.1
之后,go提供了内置的go mod
工具,通过go mod
我们可以愉快的进行go依赖包的管理和升级。
go mod 常用命令
download download modules to local cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
项目中配置go mod
在新项目中配置go mod
- 在
GOPATH外
新建一个目录,使用go mod init xxx
来初始化go.mod
文件。go