Don't spend $10k on a bootcamp. Try our back-end career path first.
Back home

Using ‘Go Generate’ To Deploy Multi-Process Apps

By Lane Wagner on Apr 22, 2020

In microservice architectures, it’s fairly common to have a project that includes different worker types. A Makefile can be used to manage the creation of multiple programs, but the Go toolchain has a tool that can be used as well, go generate . Here are some examples of how it can be used:

In other words, we have one git repository, but from that code, we want to generate ‘n’ number of executables.

Repository Structure

Our normal single-process repositories have the following structure:

As you can see, when there is only one program (one main.go) its really easy to build and deploy. We just run:

go build

from the root of the repository.

Structure With Multiple Programs

Now let’s say we have a project that has an API that is responsible for managing some long-running jobs. For example, we can pretend it manages RSS scraping jobs. Here is how we would build out the repository:

Here we have a cmd folder in the root, which then holds a directory for each executable. This allows us to still scope packages to the entire project, share a CI/CD pipeline, and keep code that is tightly-coupled all in one place.

How Do We Build It?

In the above project structure, you may have noticed the gen.go file. Here is what it looks like:

package main

//go:generate go build ./cmd/api
//go:generate go build ./cmd/worker

Now we can run go generate from the root of our project and both executables will be built at the root.

A simple path to your career in back-end development

The pace of's JavaScript, Python and Go courses has been perfect for me. The diverse community in Discord is a blast, and other members are quick to help out with detailed answers and explanations.

- Daniel Gerep from Cassia, Brasil

Get a back-end job without spending $10k on a bootcamp

I was a field service engineer and I wanted to become a backend developer, but work and family limited my options. Since completing the back-end career path on, I now have a job offer in hand and I'm starting my career as a software developer!

- Özgür Yildirim from Germany

Can We Do More?

Have more steps in the build process? Go generate is quite flexible, lets build the production docker image as well (assuming we have a Dockerfile in the root of the project):

package main

//go:generate go build ./cmd/api
//go:generate go build ./cmd/worker
//go:generate docker build .

The generate function is a powerful tool and can do a lot more than just build a list of executables. Nevertheless, we’ve found this to be a convenient way to keep our projects “go native”.

Learn back-end without spending $10,000+ on a bootcamp

Related Reading