Using a Makefile with Golang
This article was last edited over 3 years ago. Information here may no longer be accurate. Please proceed with caution, and feel free to contact me.
I’ve found that using a Makefile
is helpful for
running and building Go projects.
You can create a very complex Makefile
to coordinate
a number of build tasks. However, I will not focus on a complex
setup here. Rather, this is a quick and dirty generic
Makefile
that I like to use for demo or throwaway
projects.
You could just as easily use a bash
script, but I
prefer a Makefile
. My example will not cover all
scenarios, but I like using this as a generic build process for my
one-off standalone Go apps.
You should read up on
setting the GOPATH
to understand how to set up a reliable GOPATH
for
long term development. My Makefile
is specifically
overriding the GOPATH
to the current directory. That
is because this example Makefile
is designed for
throaway projects. This is probably not ideal for most
workflows, but that is up to you to decide.
# Makefile
export GOPATH := $(shell pwd)
all:
echo $$GOPATH
go get -d
go run *.go
build:
echo $$GOPATH
go get -d
go build -o out.bin
Then simply run make
to run your Go app.
You can also run make build
to build a binary for
your Go app.
Dependencies get downloaded to ./src
. Note, this is a
relative path. The dependencies will end up in the
current directory with your Makefile
and Go
code due to how I am setting the GOPATH
.