单体仓库(Monorepo):如何从另一个项目中使用一个包?

huangapple go评论92阅读模式
英文:

Monorepo: How to consume a package from another project?

问题

我正在尝试在Go中创建我的第一个monorepo。项目结构如下所示:
单体仓库(Monorepo):如何从另一个项目中使用一个包?

如图所示,monoplay文件夹是根目录。
pb文件夹包含了我想在srv_boo/main.gosrv_foo/main.go文件中使用的生成的gRPC代码

问题是,如何在srv_boo/main.gosrv_foo/main.go文件中使用来自pb文件夹的生成的gRPC代码
文件夹结构是否正确?

我还想单独部署这些服务。

也许https://bazel.build/是解决方案吗?

英文:

I am trying to create my first monorepo in Go. The project structure looks as follows:
单体仓库(Monorepo):如何从另一个项目中使用一个包?

As you can see on the picture, the monoplay folder is the root.
The pb folder contains the generated gRPC code that I would like to consume in the srv_boo/main.go and srv_foo/main.go files.

The question is, how to consume the generated gRPC code from folder pb in the srv_boo/main.go and srv_foo/main.go files?
Is the folder structure correct?

Would like also to deploy the services individually.

Is maybe https://bazel.build/ the solution?

答案1

得分: 2

将整个代码库作为一个go模块将有助于解决这个问题,即在"Monoplay"根目录下只有一个go.mod文件。
然后,服务可以使用"github.com//monoplay/pb/"导入来引用生成的go文件。

这样做还可以集中管理整个代码库的依赖关系,因为只有一个go.mod文件,如果这是你想要的。

其他的选择:
使用"go mod edit":
https://go.dev/ref/mod#go-mod-edit
或者,如DazWilkin建议的,使用proto文件中的"go_package",以及"go-grpc_opt"和"go_opt"。

我使用单一模块的方法,并推荐使用。

如果代码库包含大量代码,并且构建所有内容(包括容器镜像)很麻烦且时间过长,那么可以考虑使用bazel。

英文:

Having the entire repository as one go module will help with this, i.e only one go.mod file in the "Monoplay" root folder.
Then the services can reference the generated go files using "github.com/*/monoplay/pb/*" imports.<br>
This will also centralize dependency management for all the entire repository, since there is only one go.mod file, if that is something you want.

Other alternatives:<br>
Using "go mod edit":
https://go.dev/ref/mod#go-mod-edit&lt;br>
Or, as DazWilkin suggests, use "go_package" in proto files together with "go-grpc_opt" and "go_opt".

I use the single module approach and recommend it.

If the repository will contain a lot of code and building everything (including container images) is cumbersome and takes to long then look into bazel.

huangapple
  • 本文由 发表于 2022年1月20日 05:54:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/70778253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定