User Tools

Site Tools


'go build' flags and friends cheatsheet

go build [build flags] [packages]


These flags are available in the go build, clean, get, install, list, run, and test commands. Not all of these commands support all the flags (e.g. go list -i would not make sense).

Flag Role
-a Force rebuilding of packages that are already up-to-date.
-asmflags <flags> Set flags for the assembler.
-buildmode <mode> Set the build mode (exe, c-shared, c-archive, pie, plugin).
-compiler <name> Set the compiler to use (gc, gccgo).
-gcflags <flags> Set flags for the Go compiler.
-i Install the package after building it.
-ldflags <flags> Set linker flags.
-mod <mode> Set the module download mode (readonly, vendor, mod).
-o <output> Set the output file name (default is package name or current directory).
-race Enable data race detection.
-tags <tags> Set build tags.
-trimpath Remove all file system paths from the resulting binary.
-v Print the names of packages as they are compiled.
-work Print the name of the temporary work directory and keep it (for debugging).

Common build tags

Tag Purpose
netgo Use pure Go networking libraries.
osusergo Use pure Go implementation for user lookup.

Common linker flags

Flag Purpose
-extld <linker> Use a specific external linker.
-s Omit symbol table and debug information.
-w Omit DWARF debugging information.

Build modes

Build mode Description
c-archive Build a C archive library.
c-shared Build a C shared library.
exe Build a standalone executable binary (default).
pie Build a position-independent executable.
plugin Build a Go plugin.
go/buildflags.txt · Last modified: 2023/05/17 07:37 by fgm