Published on

Using a makefile to streamline the Go development process

Authors

Having worked with Go for a few months now one of the things I have missed from other languages is one file that contains the config for how you do the various things in your project. For example:

  1. Getting dependencies
  2. Building
  3. Running tests

I have noticed that quite a few Go projects use makefiles to address this problem. I came across a really cool makefile here which I have adapted slightly to fit in more with my workflow:

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=govendor fetch

all: test build
bootstrap:
	# make the vendor directory as govendor needs it
	mkdir -p vendor
	# install govendor on your path
	go get -u github.com/kardianos/govendor
	govendor init
build:
	$(GOBUILD) -v
test:
	$(GOTEST) -v ./...
clean:
	$(GOCLEAN)
	rm -f $(BINARY_NAME)
	rm -f $(BINARY_UNIX)
run:
	$(GOBUILD) -v ./...
	./$(BINARY_NAME)
deps:
	# add any other deps you need to the below
	$(GOGET) github.com/markbates/goth
	$(GOGET) github.com/markbates/pop

Ensure that you use tabs in your makefile otherwise you will get an error when trying to run it.

Once you have this makefile setup the workflow is pretty straight forward you simply run make <anyCommandFromFile>. For example the 3 from the beginning of this article would map to:

  1. make deps
  2. make build
  3. make test

You can string these tasks together however makes sense. For example make clean deps build test run. If you use zsh as your shell tabbing after make will suggest possible tasks you can use based on your makefile.