Makefile for Go projects
This is an example of
Makefile to build a Go project. This is quite
similar to Filippo Valsorda's hellogopher.
Initially, this is for people who don't know about
GOPATH or who
don't want to use it (like me). However, starting with Go 1.11,
modules enable to work outside of
GOPATH without any special
environment. This turns this
Makefile as only a convenience tool.
This example relies on modules to retrieve dependencies. This require
use of Go 1.11. To update a dependency, use
go get DEPENDENCY@REVISION.
For older versions of Go, it's possible to use
go get -u golang.org/x/vgo make GO=$GOPATH/bin/vgo
On first build, you need to run
go mod init PROJECTNAME.
Version is extracted from git tags using anything prefixed by
The following commands are available:
make helpto get help
maketo build the binary (in
make testto run tests
make test-verboseto run tests in verbose mode
make test-racefor race tests
make test-xmlfor tests with xUnit-compatible output
make test-coveragefor test coverage (will output
make test PKG=helloworld/helloto restrict test to a package
make lintto run golint
make fmtto run gofmt
The very first line of the
Makefile is the most important one: this
is the path of the package. I don't use a
go getable package path
but you can.
Be sure to browse the remaining of the
Makefile to understand what
it does. There are some tools that will be downloaded. You can use
already-installed one by specifying their full path this way instead:
make lint GOLINT=/usr/bin/golint
Files other than
Makefile are just examples.
golint complains with
go/build: importGo hellogopher/cmd: exit status 2, just delete it from
bin/ and let the Makefile build it
If you prefer, you can also include this Makefile into another one.
Rename it to
hellogopher.mk and put in
Makefile something like
include hellogopher.mk # Your custom settings TIMEOUT=10 # Your custom rules doc: ; $(info $(M) build documentation) @ ## Build documentation $(MAKE) -C doc
Makefile is published under the CC0 1.0 license. See
for more details.