So, I have a few web services written in Go and I am planning to write a few more. The services all live in their own folders outside of GOPATH and use Glide and Dep for managing vendored dependencies (I intend to convert to Go 1.11 modules in the near future).
The problem I am having is that there is a lot of code duplication in these services, making things fragile. Especially code related to database structure and access. I would like to move all the database access and other service specific code into a shared location somewhere and import it into the services as needed.
What is the right way to do that? I have been experimenting with different project structures, but I always run into one of the two problems.
If I use Go 1.11 modules, I end up having to share go.mod and go.sum between all the services. This makes the build process for individual services longer, since each service build has to fetch all of the dependencies from all the services in the same location. As my number of services will grow, this problem will become worse.
If I set GOPATH so my service code ends up inside, I run into multiple problems. The services are part of a larger project written in a different language and are part of the version control repository for the project. The structure, currently, is
- project
- services
- svc1
- svc2
- othercode
- morecode
If I set the GOPATH to services, I have to move individual services directories under a separate src, I end up with extra pkg and bin directories and every Go package I ever want to install ends up somewhere in the ~/project/services tree as well. I would rather keep using vendored dependencies.
How do I organize my code, so I avoid either of the above scenarios and still be able to import shared code from multiple services?