Monorepo: How to consume a package from another project?

Issue

I am trying to create my first monorepo in Go. The project structure looks as follows:
enter image description here

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?

Solution

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.
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:
Using "go mod edit":
https://go.dev/ref/mod#go-mod-edit
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.

Answered By – fratle

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.