今天读了golang官方文档中的module介绍
https://blog.golang.org/using-go-modules
从go1.11之后,Google应该是想把module变成正统包管理器了。在此,我简单的以工程角度探究一下mod。
首先,文档提到:
A module is a collection of Go packages stored in a file tree with a go.mod file at its root. The go.mod file defines the module’s module path, which is also the import path used for the root directory, and its dependency requirements, which are the other modules needed for a successful build.
也就是说,module是go package的集合。它定义了module path,dependencies等等。我们写一个小项目,结构如下:

我们创建一个不在GOPATH目录下的测试项目(官网中提到,GOPATH下的向后兼容,跟之前版本的管理办法一样)。我的go.mod设置的module path跟实际路径不一样,为了测试module path可不可以跟实际路径不同。结果如下:

可以看到,实际path是./hello,而go.mod

本文探讨了Go语言从1.11版本开始引入的module系统,重点在于理解module path及其在实际项目中的应用。通过创建一个独立于GOPATH的测试项目,展示了module path可以不同于实际文件路径,并不影响程序正常运行。在导入包时,遵循的是module path,而不是实际文件系统的路径。实验结果显示,import路径遵循`import path = module path + subpackage`的规则。
最低0.47元/天 解锁文章
993

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



