前言
go version 需高于1.11
- 配置环境变量
go env
GO111MODULE=on
GOPROXY=https://goproxy.cn,direct
GOSUMDB=off
-
修改GO111MODULE
-
GO111MODULE 有三个值:off, on和auto(默认值)。
-
GO111MODULE=off
go命令行将不会支持module功能,寻找依赖包的方式将会沿用旧版本那种通过vendor目录或者GOPATH模式来查找。
-
GO111MODULE=on
go命令行会使用modules,而一点也不会去GOPATH目录下查找。
-
GO111MODULE=auto
默认值,go命令行将会根据当前目录来决定是否启用module功能。这种情况下可以分为两种情形:
-
当前目录在GOPATH/src之外且该目录包含go.mod文件
当前文件在包含go.mod文件的目录下面。
当modules功能启用时,依赖包的存放位置变更为$GOPATH/pkg,允许同一个package多个版本并存,且多个项目可以共享缓存的 module
- go mod命令
golang 提供了 go mod命令来管理包。
go help mod
Go mod provides access to operations on modules.
Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.
Usage:
go mod <command> [arguments]
The commands are:
download download modules to local cache (下载依赖包)
edit edit go.mod from tools or scripts(编辑go.mod)
graph print module requirement graph (打印模块依赖图)
init initialize new module in current directory(在当前目录初始化mod)
tidy add missing and remove unused modules (拉取缺少的模块,移除不用的模块)
vendor make vendored copy of dependencies (将依赖复制到vendor下)
verify verify dependencies have expected content (验证依赖是否正确)
why explain why packages or modules are needed (解释为什么需要依赖)
Use "go help mod <command>" for more information about a command.
本文介绍了Go语言的模块管理,强调了GO111MODULE环境变量的作用,包括off、on和auto三种模式的含义。重点讲解了模块功能启用后依赖包的存储位置变化,并列举了`go mod`命令的用途,如下载、编辑、初始化、清理和验证等操作,帮助读者理解Go语言的现代包管理方式。
2866

被折叠的 条评论
为什么被折叠?



