Let’s say you want to have two Go packages pkg1
and pkg2
in a monorepo setup. Here’s what a good project structure would look like.
Here’s my recommended project structure in that case
|
|
src/pkg2
’s structure would look very similar.
Now let’s assume that you are hosting this monorepo at https://github.com/ashishb/golang-template-repo
. I would recommend that src/pkg1
’s module name, defined in go.mod
be github.com/ashishb/golang-template-repo/src/pkg1
and pkg2’s module name be github.com/ashishb/golang-template-repo/src/pkg2
. This is not a hard-and-fast requirement, however, this will avoid a lot of confusion if the package name maps to the URL.
Now, let’s assume that there is code in src/pkg1/pkg/module1
that’s exported and is being used by pkg2
. To add that mapping just use the replace directive and add the following to src/pkg2/go.mod
.
|
|
Now, import github.com/ashishb/golang-template-repo/src/pkg1/pkg/module1
would just work.
As a reminder, import github.com/ashishb/golang-template-repo/src/internal
would never work as internal
is never exported.