Toolkit: gopls, go mod edit, GoReleaser & Proxies
The go command does most of the work in this course. Around it sit a few tools a build engineer meets every week: the language server in everyone's editor, scripting interfaces for go.mod, a release tool, and module proxy servers.
gopls: the language server
gopls (pronounced "go please") powers Go support in VS Code, GoLand's alternatives, Neovim, Emacs and others. It loads your module (or workspace) the same way the go command does, so module problems show up in the editor first: "no required module provides package", a missing go.sum entry, or a file shown as "excluded by build constraints".
Install it like any personal tool: go install golang.org/x/tools/gopls@latest. Settings that matter for builds:
{
"gopls": {
"buildFlags": ["-tags=pro,integration"],
"env": { "GOFLAGS": "-mod=mod" },
"ui.diagnostic.analyses": { "unusedparams": true }
}
}buildFlagswith-tagsmakes files behind build tags visible to the editor.- With several modules in one repository, open the folder that holds a
go.work(or create one): gopls then treats every listed module as part of one view, and cross-module navigation works withoutreplace. - gopls offers quick fixes for module issues ("run go mod tidy", "upgrade dependency") and code lenses in
go.modthat check for upgrades and vulnerabilities.
go mod edit and go list: go.mod for scripts
Never parse go.mod with sed or regular expressions in scripts: its grammar has blocks, comments and quoting. Use the go command's machine interfaces instead:
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"github.com/fatih/color"\n\t"rsc.io/quote"\n)\n\nfunc main() { fmt.Println(quote.Go(), color.NoColor) }\n' > main.go
go mod tidy 2>/dev/null
echo "--- the requirements as written in go.mod"
go mod edit -json | sed -n '/"Require"/,/]/p'
echo "--- the build list, through a template"
go list -m -f '{{if not .Main}}{{.Path}} {{.Version}}{{if .Indirect}} (indirect){{end}}{{end}}' all
echo "--- module paths of every package the build compiles"
go list -deps -f '{{with .Module}}{{.Path}}{{end}}' . | sort -u
echo "--- preview an edit without writing it"
go mod edit -require=rsc.io/quote@v1.5.1 -print | grep 'rsc.io/quote'
echo "--- a hand-made line: -fmt formats it, but only go commands make it consistent"
printf 'require rsc.io/sampler v1.3.1\n' >> go.mod
go mod edit -fmt && tail -1 go.mod
go build . ; go mod tidy && grep sampler go.mod--- the requirements as written in go.mod
"Require": [
{
"Path": "github.com/fatih/color",
"Version": "v1.19.0"
},
{
"Path": "rsc.io/quote",
"Version": "v1.5.2"
},
{
"Path": "github.com/mattn/go-colorable",
"Version": "v0.1.14",
"Indirect": true
},
{
"Path": "github.com/mattn/go-isatty",
"Version": "v0.0.20",
"Indirect": true
},
{
"Path": "golang.org/x/sys",
"Version": "v0.42.0",
"Indirect": true
},
{
"Path": "golang.org/x/text",
"Version": "v0.0.0-20170915032832-14c0d48ead0c",
"Indirect": true
},
{
"Path": "rsc.io/sampler",
"Version": "v1.3.0",
"Indirect": true
}
]
--- the build list, through a template
github.com/fatih/color v1.19.0
github.com/mattn/go-colorable v0.1.14 (indirect)
github.com/mattn/go-isatty v0.0.20 (indirect)
golang.org/x/sys v0.42.0 (indirect)
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c (indirect)
rsc.io/quote v1.5.2
rsc.io/sampler v1.3.0 (indirect)
--- module paths of every package the build compiles
example.com/app
github.com/fatih/color
github.com/mattn/go-colorable
github.com/mattn/go-isatty
golang.org/x/sys
golang.org/x/text
rsc.io/quote
rsc.io/sampler
--- preview an edit without writing it
rsc.io/quote v1.5.1
--- a hand-made line: -fmt formats it, but only go commands make it consistent
require rsc.io/sampler v1.3.1
go: updates to go.mod needed; to update it:
go mod tidy
go: downloading rsc.io/sampler v1.3.1
rsc.io/sampler v1.3.1 // indirect
| Interface | Use it for |
|---|---|
go mod edit -json |
read go.mod as written (requirements, replaces, tools…) |
go mod edit -print |
preview an edit without writing it |
go mod edit -fmt |
canonical formatting (what go writes) |
go list -m -json all / -f '{{…}}' |
the resolved build list with versions, directories, replacements |
go list -deps -json ./... |
packages, their files, imports and modules |
go env -json |
the effective configuration (GOPROXY, GOFLAGS, GOENV…) |
GoReleaser (not run here)
GoReleaser automates the release loop from Cross Builds & cgo and Version Stamping: for a Git tag it builds every target, stamps versions, creates archives and checksums, writes a changelog and publishes a GitHub/GitLab release, container images and package-manager manifests. A minimal .goreleaser.yaml:
version: 2
builds:
- main: ./cmd/app
env: [CGO_ENABLED=0]
goos: [linux, darwin, windows]
goarch: [amd64, arm64]
flags: [-trimpath]
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}}
mod_timestamp: "{{ .CommitTimestamp }}" # reproducible file times
archives:
- formats: [tar.gz]
format_overrides:
- goos: windows
formats: [zip]
checksum:
name_template: checksums.txtLocally, goreleaser release --snapshot --clean builds everything into dist/ without publishing; in CI a tag push runs goreleaser release. Note the mod_timestamp and the absence of the build time in ldflags: both keep the artifacts reproducible. Tagging, changelogs and CI pipelines in general are the subject of Release Engineering.
Module proxy servers (not run here)
| Server | Notes |
|---|---|
| proxy.golang.org | Google's public mirror and the default GOPROXY; caches public modules forever, so builds survive deleted repositories |
| Athens | open-source Go proxy; storage on disk, S3, GCS, Azure, MongoDB…; fetches private modules with its own credentials; filters (allow/deny lists) |
| Artifactory, Nexus Repository | generic artifact servers with Go repository types; "virtual" repositories combine a private store and a public proxy behind one URL |
| GOPROXY directory | any static file server (or file:// path) laid out like gopublish writes in Upgrades, exclude & retract; good for air-gapped mirrors |
| goproxy.io, goproxy.cn | public mirrors used where proxy.golang.org is slow or blocked |
A typical Athens setup for a company points developers at it and lets it reach private Git with a token:
# developers / CI
GOPROXY=https://athens.corp.example
GOPRIVATE=*.corp.example
GONOPROXY=none
# athens config.toml (excerpt)
StorageType = "disk"
GoBinaryEnvVars = ["GOPRIVATE=*.corp.example", "GOFLAGS=-mod=mod"]
NETRCPath = "/etc/athens/.netrc"Private Modules & Proxies explains why GONOPROXY=none belongs with a company proxy.
Test yourself
How should a script read the requirements in go.mod?
- grep and sed
go mod edit -json(orgo list -m -json allfor the resolved build list)- Parse it as TOML
- Read go.sum instead
Files behind //go:build pro show as excluded in your editor. What fixes it?
- Delete the build tag
- Set gopls
buildFlagsto-tags=pro - Run go mod tidy
- Add the files to go.work
What does a caching module proxy such as Athens or Artifactory add to a company's builds?
- Faster compilation
- One controlled source for public and private modules that keeps working when upstream repositories disappear
- Automatic upgrades
- Code signing