A Makefile for your Go project (2019)
My most loathed feature of Go was the mandatory use of GOPATH
:
I do not want to put my own code next to its dependencies. I was not
alone and people devised tools or crafted their own Makefile
to
avoid organizing their code around GOPATH
.
Hopefully, since Go 1.11, it is possible to use Go’s modules to
manage dependencies without relying on GOPATH
. First, you need to
convert your project to a module:1
$ go mod init hellogopher go: creating new go.mod: module hellogopher $ cat go.mod module hellogopher
Then, you can invoke the usual commands, like go build
or go test
.
The go
command resolves imports by using versions listed in
go.mod
. When it runs into an import of a package not present in
go.mod
, it automatically looks up the module containing that package
using the latest version and adds it.
$ go test ./... go: finding github.com/spf13/cobra v0.0.5 go: downloading github.com/spf13/cobra v0.0.5 ? hellogopher [no test files] ? hellogopher/cmd [no test files] ok hellogopher/hello 0.001s $ cat go.mod module hellogopher require github.com/spf13/cobra v0.0.5
If you want a specific version, you can Continue reading