How to use Makefiles for your Golang development

Hello, in this little post I would like to show you how you can use Makefiles to ease the development of your Golang programs.

Image for post
Image for post

“go <command>s” are great by themselves, you can basically manage every part of your Golant development phase with Go’s native tools, so why would I want to use something else? This is a fair question. In many cases I have had the necessity of sharing my codebases or repositories with someone that is not familiar with Golang itself, they won’t know how to build it. Of course you can attack that problem with some documentation in your or some other files specifying how to build your program. A makefile is a great way of abstracting the technical details for someone that just want to try to build our program quickly, in an agnostic way. At the exact moment I see that there is a “Makefile”, I immediately know that there is something I can build with make and optionally with make install.

They are specially useful when you want to reduce your typing or you just want to avoid typos.

Instead of typing this:

go build -o myprogram

I can automate these steps with a custom target such as follows:

Go program (example)

package mainimport "fmt"func main() {
fmt.Println("Hello Make!")


.DEFAULT_GOAL := buildandrunBIN_FILE=myprogrambuildandrun:
@go build -o "${BIN_FILE}"

then you just need to run the following command to build and run your program:

$ make
Hello Make!

and that is it. Using the “go build …” version requires ~33 characters in my case to build and run the program, I have reduced that to only 4 characters (“make”). This becomes specially useful when you want to abstract multiple things we usually do when we are on a really active development phase, such as: cleaning, running tests, check test coverage, running a linter, etc. I am aware that I can just use the IDE for most of these things or perhaps just use Shell Scripts, but IMO, using Makefiles seems very convenient, I am also aware that if you work a lot in the Terminal or the CLI, you can also use the Bash history for many of these things.

My custom Makefile for Go development

This is the Makefile I usually use for when I am working on my Golang projects, it is not very complex, yet useful.

.DEFAULT_GOAL := buildBIN_FILE=littleubuild:
@go build -o "${BIN_FILE}"
go clean
rm --force "cp.out"
rm --force nohup.out
go test
go test
go test -coverprofile cp.out
go tool cover -html=cp.out
golangci-lint run --enable-all

These are the targets and their purposes:

“make”: without any target after the “make” command will run my default target, in this case “build”.

“clean”: runs “go clean” and then removes some files such as “cp.out” (resulting file from running test coverage) and”nohup.out”, in case there is a “nohup.out”, result from a “nohup” command execution.

“test”: to run the tests.

“check”: check is basically a synonym for the “test” target.

“cover”: runs test coverage.”run”: to run the program.

“lint”: to run the linter.

Image for post
Image for post
Building and running my program

Chances are that if you are running any *nix/Linux/macOS operating system, Make will be installed and you can start using it and benefit from it.


I think I benefit a lot from simple Makefiles to manage the building of my Golang programs, I use them to reduce the typing (and typos), also to abstract complex building commands into simple words, such as “lint”, “cover”, “run” or “clean”. Also, since Make is basically ubiquitous, I am confident that any person with some programming background coming from using *nix/Linux/macOS, will know that to build my program, they just want to run “make” (or take a quick look to the Makefile content) to do something. Do you use Makefiles for your Golang programs? if so, how? please let me know in the comments section.

Thank you!

Hi! I am Leonardo Gutiérrez R., a passionate software developer, I hope you find the content in this blog interesting. I write about IT related stuff.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store