咱们都知道在使用Golang时开发程序时都须要在 GOPATH
下面,这就很是不方便。若是你想放在磁盘上的其余地方,那么go mod将是你的“好伙伴”。git
关于 go mod 的说明,能够参考:github
➜ ~ go 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 下载依赖的 module 到本地 cache edit edit go.mod from tools or scripts 编辑 go.mod graph print module requirement graph 打印模块依赖图 init initialize new module in current directory 在当前目录下初始化 go.mod(就是会新建一个 go.mod 文件) tidy add missing and remove unused modules 整理依赖关系,会添加丢失的 module,删除不须要的 module 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.
set GO111MODULE=ON
go mod init
在当前目录下生成一个go.mod
文件,若是以前有生成过须要删除再初始化执行完上面步骤基本就完成了,运行下程序你会发现目录下多了一个go.sum
文件,是用来记录所依赖的版本的锁定golang
执行命令go mod verify
命令来检查当前模块的依赖是否所有下载下来,是否下载下来被修改过。若是全部的模块都没有被修改过,那么执行这条命令以后,会打印all modules verified
。ide
使用go mod
后你会发如今GOPATH
下面的pkg
目录会有一个mod
目录,里面包含了项目须要的依赖包,这也是为何不须要再GOPATH
中开发程序也能使用的缘由ui