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 Makefile
may not be used as is. It is expected to be modified
to fit your needs. See Akvorado's
Makefile for
an example on a more complex project.
This example relies on modules to retrieve dependencies. This requires use of Go
1.24 or more recent. To update a dependency, use go get DEPENDENCY@REVISION
or
go get -tool DEPENDENCY@REVISION
if this is a tool.
You can checkout tag v0.6 if you need compatibility down to Go 1.11 or tag v1.4 if you need compatibility down to Go 1.16.
Some tools now require more recent versions of Go (1.22+), but it would be possible to pin them to older versions.
On first build, you need to run go mod init PROJECTNAME
.
Version is extracted from git tags using anything prefixed by v
.
The following commands are available:
make help
to get helpmake
to build the binary (inbin/
)make test
to run testsmake test-verbose
to run tests in verbose modemake test-race
for race testsmake test-coverage
for test coverage (will outputcoverage.html
andcoverage.xml
intest/
.make test PKG=helloworld/hello
to restrict test to a packagemake clean
make lint
to run golintmake fmt
to run gofmt
Be sure to browse the Makefile
to understand what it does. Files other than
.gitignore
and Makefile
are just examples.
If you prefer, you can also include this Makefile into another one.
Rename it to hellogopher.mk
and put in Makefile
something like
this:
include hellogopher.mk
# Your custom settings
TIMEOUT=10
# Your custom rules
doc: ; $(info $(M) build documentation) @ ## Build documentation
$(MAKE) -C doc
This Makefile
is published under the CC0 1.0 license. See LICENSE
for more details.