[Golang] go mod及module path初级探究

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

今天读了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目录下的测试项目(官网中提到,GOPATH下的向后兼容,跟之前版本的管理办法一样)。我的go.mod设置的module path跟实际路径不一样,为了测试module path可不可以跟实际路径不同。结果如下:
这是go.mod内容
可以看到,实际path是./hello,而go.mod

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值