LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

go-build

compile Go packages and dependencies

TLDR

Build current package
$ go build
copy
Build specific file
$ go build [main.go]
copy
Set output name
$ go build -o [binary-name]
copy
Cross-compile
$ GOOS=[linux] GOARCH=[amd64] go build
copy
Build with race detector
$ go build -race
copy
Build with trimmed paths (for reproducible builds)
$ go build -trimpath -o [binary-name]
copy
Build with profile-guided optimization
$ go build -pgo=[profile.pprof] -o [binary-name]
copy

SYNOPSIS

go build [options] [packages]

DESCRIPTION

go build compiles Go packages and dependencies. It produces an executable binary from the main package or checks compilation for library packages.The command handles dependency resolution, compilation, and linking. Cross-compilation is built-in via GOOS and GOARCH environment variables, requiring no additional toolchains.

PARAMETERS

PACKAGES

Packages to build.
-o FILE
Output file name.
-v
Verbose output.
-race
Enable race detector.
-ldflags FLAGS
Linker flags.
-tags TAGS
Build tags.
-trimpath
Remove all file system paths from the resulting executable.
-gcflags '[pattern=]arg list'
Arguments passed to the Go compiler.
-mod MODE
Module download mode: readonly, vendor, or mod.
-pgo FILE
Profile-guided optimization file (default: auto).
-cover
Enable code coverage instrumentation.
-a
Force rebuilding of packages already up-to-date.
-n
Print commands but do not run them.
-x
Print the commands as they are executed.
-buildmode MODE
Build mode (default, archive, c-archive, c-shared, shared, exe, pie, plugin).

CAVEATS

Main package produces an executable; library packages only check compilation. CGO requires a C compiler. Use `go help build` for full documentation.

HISTORY

go build is a core command of the Go toolchain, providing fast incremental compilation since Go's release.

SEE ALSO

go(1), go-run(1), go-install(1)

Copied to clipboard
Kai