英文:
Monorepo: How to consume a package from another project?
问题
我正在尝试在Go中创建我的第一个monorepo。项目结构如下所示:
如图所示,monoplay
文件夹是根目录。
pb
文件夹包含了我想在srv_boo/main.go
和srv_foo/main.go
文件中使用的生成的gRPC代码
。
问题是,如何在srv_boo/main.go
和srv_foo/main.go
文件中使用来自pb
文件夹的生成的gRPC代码
?
文件夹结构是否正确?
我还想单独部署这些服务。
也许https://bazel.build/是解决方案吗?
英文:
I am trying to create my first monorepo in Go. The project structure looks as follows:
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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论