Skip to content
Latchkey

go build: Usage, Options & Common CI Errors

Compile Go packages into a binary or just check they build.

go build compiles the named packages and, for a main package, links an executable. Cross-compilation is controlled by the GOOS and GOARCH environment variables.

What it does

Compiles packages and their dependencies. For a main package it writes a binary (named after the directory unless -o is given); for library packages it only checks that they compile and discards output.

Common usage

Terminal
go build ./...                    # build every package
go build -o bin/app ./cmd/app     # named output binary
GOOS=linux GOARCH=arm64 go build -o app .
go build -ldflags "-s -w" .       # strip symbols (smaller binary)

Common CI error: missing module for a package

go build ./... fails with "no required module provides package example.com/x; to add it: go get example.com/x". The import is not in go.mod. Fix: run go get example.com/x then go mod tidy and commit go.mod and go.sum, so CI builds with the dependency present.

Options

Flag / EnvEffect
-o <path>Output file or directory
./...All packages under the module
GOOS / GOARCHTarget OS / architecture
-ldflags <flags>Pass flags to the linker
-tags <list>Set build tags

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →