Currently, there are 2 options for Go projects:
go-workspace.mk
: For GOPATH workspacesgo-mod.mk
: For Go 1.11 modules
Besides the differences outlined in this section, go-workspace.mk
and go-mod.mk
should work more-or-less identically.
$ MODULE=EXAMPLE.COM/YOU/GITREPO
$ echo 'include build-aux/go-workspace.mk' >> Makefile
$ echo /.go-workpsace >> .gitignore
$ echo "!/.go-workspace/src/${MODULE}" >> .gitignore
$ mkdir -p $(dirname .go-workspace/src/${MODULE})
$ ln -s $(dirname .go-workspace/src/${MODULE} | sed -E 's,[^/]+,..,g') .go-workspace/src/${MODULE}
What's that big expression in the ln -s
command!? It's the same as
$ ln -sr . .go-workspace/src/${MODULE}
but for lame operating systems that ship an ln
that doesn't
understand the -r
flag.
$ go mod init EXAMPLE.COM/YOU/GITREPO
$ echo 'include build-aux/go-mod.mk' >> Makefile
$ go mod init EXAMPLE.COM/YOU/GITREPO
$ make clobber
$ rm -rf -- .go-workspace vendor glide.* Gopkg.*
$ sed -i -E 's,/go-workspace\.mk,/go-mod.mk,' Makefile
$ sed -i -e '/\.go-workspace/d' .gitignore
go-{mod,workspace}.mk
try hard to automatically find whatever source
files you write, and do the right thing for each of the standard
common.mk
targets. By default:
build
: Anypackage "main"
package will automatically get build, and placed in./bin_$(GOOS)_$(GOARCH)
(assuming the+build
tags pass). If you have any helper scripts that you intend to run withgo run
, and don't want to be compiled, you should put// +build ignore
in them.check
: All packages in your project are automatically tested withgo test
.lint
: All packages in your project are automatically linted withgo vet
, and are verified to be formatted withgofmt -s
.- When using
go-workspace.mk
, thevendor
directory is created automatically, using either Glide (ifglide.yaml
exists), ordep
(ifGopkg.toml
exists).
Knobs you can turn in your Makefile
:
go.PLATFORMS
: A list ofGOOS_GOARCH
pairs to compile executables for. Just$(go env GOOS)_$(go env GOARCH)
by default (that is, the host platform, unless you've set theGOOS
orGOARCH
environment variables).go.DISABLE_GO_TEST
: If set to a non-empty string, don't havemake check
rungo test
. This is useful if you need to pass special flags togo test
, and would like to write the rule yourself.make lint
behavior can be modified either by creating a.golangci.yml
,.golangci.toml
,.golangci.json
file; or by settinggo.GOLANG_LINT_FLAGS
. See thegolangci-lint
docs for information on that file format, and what valid flags are.go.LDFLAGS
can specify any flags to pass to the Go linker.go-version.mk
uses this to set themain.Version
symbol.
If you include build-aux/go-version.mk
, it will set go.LDFLAGS
to
include a main.Version
symbol, which a string set to the
$(VERSION)
Make variable
go.LDFLAGS += -X main.Version=$(VERSION)
If you do not set VERSION
in your Makefile
, it will be set
automatically by version.mk
.