I lead a team that is responsible for anywhere from 15-25 Go microservices at any given time. We are constantly creating new services and libraries, so it’s become important to streamline the boilerplating process. I’m mostly writing this article for myself as a kind of self-documentation, but hopefully, you’ll find it useful as you create new Go projects for yourself.
I’ve certainly found it useful in spinning up side projects like the Qvault app.
I’m a sucker for the GOPATH. Even though it’s no longer necessary, I find the single workspace principle neat and orderly. As such, I keep all my projects in:
~/go/src/REMOTE/NAMESPACE/PROJECT
The fastest way to start a new project is to hop into Github, Gitlab, or wherever my source control is located and create a new project with the default README. Then I can simply pull it down using go get
and my remote Git connection is already setup. For example:
go get github.com/qvault/webapp
As long as that command is run outside of an existing go module, the source code is cloned to the proper location in your GOPATH.
After running cd
into our new project, we create an internal
directory that will store packages that are meant to be scoped to this project. It will usually contain packages like database
, kafka
, bayesian
, etc. If you didn’t know, the [internal](https://qvault.io/2020/03/29/how-to-separate-library-packages-in-go/)
directory is a Go convention, and doesn’t allow the Go compiler to accidentally use its packages in an external project.
Next, we create a cmd
folder where we will store all the executable main
packages that will be built at compile time. Each executable will have its own directory in cmd
.
We also need a Makefile
that will automatically build all the executables in cmd
, run tests, do linting etc. It looks something like this:
test:
go test ./...
vet:
go vet ./...
lint:
test -z $(gofmt -l internal cmd)
build:
go build ./cmd/api
clean:
rm api
#engineering practices #golang #languages #cleancode #go #golang