5

发布一个自己的 go module 模块

 3 years ago
source link: https://segmentfault.com/a/1190000039835294
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

发布一个自己的 go module 模块

发布于 2 分钟前

当您希望让其他开发人员可以使用某个模块时,可以发布该模块,以便它对于 Go 工具是可见的。一旦您发布了模块,导入其包的开发人员将能够通过运行诸如 go get 之类的命令来解决模块上的依赖关系。

注意: 在发布模块之后,不要更改已标记的版本。对于使用该模块的开发人员,Go 工具对下载的模块进行身份验证,并与下载的第一个副本进行对比。如果两者不同,Go 工具将返回一个安全错误。不要更改以前发布的版本的代码,而是发布一个新版本。

有关模块开发的概述,请参见Developing and publishing modules 开发和发布模块
For a high-level module development workflow -- which includes publishing -- see 有关高级模块开发工作流(包括发布) ,请参见Module release and versioning workflow 模块发布和版本化工作流程.
Publishing steps
发布步骤

使用以下步骤发布模块。

打开命令提示符,并在本地存储库中更改到模块的根目录。

运行 go mod tidy,它删除模块可能累积的不再需要的任何依赖项。

$ go mod tidy

Run go test ./... a final time to make sure everything is working.

运行 go 测试。/... 最后一次确保一切正常。

This runs the unit tests you've written to use the Go testing framework.

这将运行您为使用 Go 测试框架而编写的单元测试。

$ go test ./...
ok example.com/mymodule 0.015s

使用 git Tag 命令为项目标记一个新的版本号。

对于版本号,请使用一个数字,向用户发出此版本中更改的性质的信号。有关更多信息,请参见模块版本编号。

$ git commit -m "mymodule: changes for v0.1.0"
$ git tag v0.1.0
Push the new tag to the origin repository.

将新标记推送到原始存储库。

$ git push origin v0.1.0

通过运行 Go list 命令来提示 Go,使模块可用,以便使用关于正在发布的模块的1nformation 更新其模块索引。

在命令之前加上一条语句,将 GOPROXY 环境变量设置为 Go 代理。这将确保您的请求到达代理。

$ GOPROXY=proxy.golang.org go list -m example.com/[email protected]

对您的模块感兴趣的开发人员可以从它导入一个包,然后像运行其他模块一样运行 go get 命令。他们可以运行最新版本的 go get 命令,也可以指定一个特定的版本,如下例所示:

$ go get example.com/[email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK