Search code examples
gogo-modules

How to import a local package from different directory in Go Modules?


I have a project which includes 6 microservices. Every microservices are in its own directory and they are Go modules, like:

- user-ms
- project-ms
- messages-ms

There are some duplicated codes in all microservices, like mongo methods, some configs, and some structs etc. I want to merge all the duplicated codes into a package. Then, I am willing to import the package on our microservices. Creating a repository on Github is impossible for me due to company restrictions.

If I create a folder in $GOPATH/src/, I don't know how to include it on docker-compose.yml.

Is it possible to create a folder ("common-utils") then import on the other modules?

- user-ms
-- go.mod
- project-ms
-- go.mod
- messages-ms
-- go.mod
- common-utils
-- go.mod

Solution

  • You can replace your package name with local package.

    go mod edit -replace github.com/remote_package=../local_package
    
    go mod tidy