19

Go Web小技巧(四)在单个仓库中支持多个 go mod 模块 | Mohuishou

 3 years ago
source link: https://lailin.xyz/post/auxvv1.html?
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 Web小技巧(四)在单个仓库中支持多个 go mod 模块

最近在更新内部的工具库的时候发现,工具仓库中其实包含了很多的模块,但是现在的版本发布都是合并在在一起发的,为了管理更加细致和直观,所以想要在一个仓库中实现实现多个模块的发布。

  1. 单个仓库中包含多个 go module 模块
  2. 版本发布可以根据模块来发版,而不是整个仓库进行发版

假设我们现在有一个仓库 github.com/mohuishou/go-test-multi-module 目录结构如下图所示

.
├── a
│   ├── a.go
│   └── go.mod
└── b
    ├── b.go
    └── go.mod

其中 a/go.mod 使用如下命令生成

go mod init github.com/mohuishou/go-test-multi-module/a

对模块进行发版时,只需打上 [模块名]/版本号 即可
以我们的示例为例,对模块 a 进行发版时我们只需要打上 tag a/v1.0.0 ,同理对模块 b 进行发版时,需要打上 tag b/v1.0.0 即可

用户在安装的时候只需要和普通模块一样执行命令即可

go get -u github.com/mohuishou/go-test-multi-module/a

一般情况下不推荐采用这种方式,还是一个仓库一个模块比较好,便于管理,但是对于一些工具库里面包含了较多的工具,想要不同类型单独发版也可以采用本文提到的方式


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK