GO-BUILD(1)

πŸ’– go-build(1) πŸ’–

okay bestie, welcome to the Go programming tools and commands manual! this page is literally all about go-build β€” compiling packages and dependencies. no cap, it's a whole vibe. let's go! βœ¨πŸ¦‹


✨ NAME ✨

go-build β€” compile packages and dependencies πŸ˜ŒπŸ’…

πŸ“œ SYNOPSIS πŸ“œ

go build [-o output] [build flags] [packages]

πŸ’… DESCRIPTION πŸ’…

okay so build compiles the packages named by the import paths, along with their dependencies, but it does not install the results. it's giving "build only, no install". periodt. πŸ’…

If the arguments to build are a list of .go files from a single directory, build treats them as a list of source files specifying a single package. so yeah, one directory, one package β€” bestie behavior. πŸŽ€

When compiling packages, build ignores files that end in _test.go. not me ignoring my tests too tho πŸ˜­πŸ™ˆ

When compiling a single main package, build writes the resulting executable to an output file named after the last non-major-version component of the package import path. The .exe suffix is added when writing a Windows executable. So go build example/sam writes sam or sam.exe. go build example.com/foo/v2 writes foo or foo.exe, not v2.exe. iconic naming logic, fr fr ✨

When compiling a package from a list of .go files, the executable is named after the first source file. go build ed.go rx.go writes ed or ed.exe. first file gets the crown πŸ‘‘

When compiling multiple packages or a single non-main package, build compiles the packages but discards the resulting object, serving only as a check that the packages can be built. so it's basically a vibe check for your code: "can this build? yes? slay." πŸ”₯

The -o flag forces build to write the resulting executable or object to the named output file or directory, instead of the default behavior described in the last two paragraphs. If the named output is an existing directory or ends with a slash or backslash, then any resulting executables will be written to that directory. so -o is your "I want it here, actually" flag. πŸ’–

✨ OPTIONS ✨

ok so listen πŸ‘‘ these build flags are shared by the build, clean, get, install, list, run, and test commands. one flag list, seven commands, we love a multipurpose queen πŸ’…

  • -C dir β€” change to dir before running the command. any files named on the command line get interpreted after the directory change. and bestie, heads up: if you use this, it has to be the first flag in the command line. no exceptions. it's giving line leader energy πŸŽ€
  • -a β€” force rebuilding of packages that are already up-to-date. yeah we're redoing the whole thing on purpose, no notes πŸ”₯
  • -n β€” print the commands but do not actually run them. pretend mode ✨
  • -p n β€” the number of programs (like build commands or test binaries) that can be run in parallel. default is GOMAXPROCS, which is normally the number of CPUs you've got available 🧚
  • -race β€” enable data race detection. supported only on darwin/amd64, darwin/arm64, freebsd/amd64, linux/amd64, linux/arm64 (only for 48-bit VMA), linux/ppc64le, linux/riscv64 and windows/amd64. check your platform first, don't get your hopes up πŸ’€
  • -msan β€” enable interoperation with memory sanitizer. supported only on linux/amd64, linux/arm64, linux/loong64, freebsd/amd64, and only with Clang/LLVM as the host C compiler. PIE build mode will be used on all platforms except linux/amd64. so specific, so particular 🫢
  • -asan β€” enable interoperation with address sanitizer. supported only on linux/arm64, linux/amd64, linux/loong64. supported on linux/amd64 or linux/arm64 and only with GCC 7 and higher or Clang/LLVM 9 and higher. and supported on linux/loong64 only with Clang/LLVM 16 and higher. read that twice bestie πŸ“‹
  • -cover β€” enable code coverage instrumentation. we're watching πŸ‘€
  • -covermode set,count,atomic β€” sets the mode for coverage analysis. default is β€œset” unless -race is enabled, in which case it's β€œatomic”. the values:
    • set: bool: does this statement run?
    • count: int: how many times does this statement run?
    • atomic: int: count, but correct in multithreaded tests; significantly more expensive.
    • and yeah, this sets -cover for you πŸ’–
  • -coverpkg pattern1,pattern2,pattern3 β€” for a build that targets package main (like, building a Go executable), apply coverage analysis to each package whose import path matches the patterns. default is to apply coverage analysis to packages in the main Go module. peep go help packages for a description of package patterns. sets -cover. πŸ’…
  • -v β€” print the names of packages as they are compiled. name-dropping, but make it compiler β˜•
  • -work β€” print the name of the temporary work directory and do not delete it when exiting. keep the receipts 🧾
  • -x β€” print the commands. loud and proud ✨
  • -asmflags [pattern=]arg list β€” arguments to pass on each go tool asm invocation.
  • -buildmode mode β€” build mode to use. see go help buildmode for more.
  • -buildvcs β€” whether to stamp binaries with version control information (β€œtrue”, β€œfalse”, or β€œauto”). by default (β€œauto”), version control information is stamped into a binary if the main package, the main module containing it, and the current directory are all in the same repository. use -buildvcs=false to always omit version control information, or -buildvcs=true to error out if version control information is available but cannot be included due to a missing tool or ambiguous directory structure. vcs drama, we've all been there 😭
  • -compiler name β€” name of compiler to use, as in runtime.Compiler (gccgo or gc).
  • -gccgoflags [pattern=]arg list β€” arguments to pass on each gccgo compiler/linker invocation.
  • -gcflags [pattern=]arg list β€” arguments to pass on each go tool compile invocation.
  • -installsuffix suffix β€” a suffix to use in the name of the package installation directory, so your output stays separate from default builds. if you're using the -race flag, the install suffix is automatically set to race or, if set explicitly, has _race appended to it. same deal for the -msan and -asan flags. using a -buildmode option that requires non-default compile flags has a similar effect. it just ✨knows✨
  • -json β€” emit build output in JSON suitable for automated processing. see go help buildjson for the encoding details.
  • -ldflags [pattern=]arg list β€” arguments to pass on each go tool link invocation.
  • -linkshared β€” build code that will be linked against shared libraries previously created with -buildmode=shared. ms. linked-the-shared-libraries πŸ’…
  • -mod mode β€” module download mode to use: readonly, vendor, or mod. by default, if a vendor directory is present and the go version in go.mod is 1.14 or higher, the go command acts as if -mod=vendor were set. otherwise, the go command acts as if -mod=readonly were set. see https://go.dev/ref/mod#build-commands for details.
  • -modcacherw β€” leave newly-created directories in the module cache read-write instead of making them read-only. free the directories πŸ¦‹
  • -modfile file β€” in module aware mode, read (and possibly write) an alternate go.mod file instead of the one in the module root directory. a file named β€œgo.mod” must still be present in order to determine the module root directory, but it is not accessed. when -modfile is specified, an alternate go.sum file is also used: its path is derived from the -modfile flag by trimming the β€œ.mod” extension and appending β€œ.sum”. sneaky but valid πŸŽ€
  • -overlay file β€” read a JSON config file that provides an overlay for build operations. the file is a JSON object with a single field, named Replace, that maps each disk file path (a string) to its backing file path, so that a build will run as if the disk file path exists with the contents given by the backing file paths, or as if the disk file path does not exist if its backing file path is empty. support for the -overlay flag has some limitations: importantly, cgo files included from outside the include path must be in the same directory as the Go package they are included from, overlays will not appear when binaries and tests are run through go run and go test respectively, and files beneath GOMODCACHE may not be replaced. read the fine print bestie, fr fr πŸ“–
  • -pgo file β€” specify the file path of a profile for profile-guided optimization (PGO). when the special name β€œauto” is specified, for each main package in the build, the go command selects a file named β€œdefault.pgo” in the package's directory if that file exists, and applies it to the (transitive) dependencies of the main package (other packages are not affected). special name β€œoff” turns off PGO. the default is β€œauto”. ~~optimization era~~ optimization era πŸ”₯
  • -pkgdir dir β€” install and load all packages from dir instead of the usual locations. for example, when building with a non-standard configuration, use -pkgdir to keep generated packages in a separate location. new home, same packages 🏠
  • -tags tag,list β€” a comma-separated list of additional build tags to consider satisfied during the build. for more information about build tags, see go help buildconstraint. (earlier versions of Go used a space-separated list, and that form is deprecated but still recognized.) deprecated but still recognized... we respect our elders πŸ’€
  • -trimpath β€” remove all file system paths from the resulting executable. instead of absolute file system paths, the recorded file names will begin either a module path@version (when using modules), or a plain import path (when using the standard library, or GOPATH). minimalism, but for paths βœ‚οΈ
  • -toolexec cmd args β€” a program to use to invoke toolchain programs like vet and asm. for example, instead of running asm, the go command will run cmd args /path/to/asm <arguments for asm>. the TOOLEXEC_IMPORTPATH environment variable will be set, matching go list -f {{.ImportPath}} for the package being built. middle-man with main character energy πŸ‘‘

🧡 the flags-with-patterns deep lore

ok so the -asmflags, -gccgoflags, -gcflags, and -ldflags flags accept a space-separated list of arguments to pass to an underlying tool during the build. to embed spaces in an element in the list, surround it with either single or double quotes πŸ“

the argument list may be preceded by a package pattern and an equal sign, which restricts the use of that argument list to the building of packages matching that pattern (see go help packages for a description of package patterns). without a pattern, the argument list applies only to the packages named on the command line.

the flags may be repeated with different patterns in order to specify different arguments for different sets of packages. if a package matches patterns given in multiple flags, the latest match on the command line wins. πŸ† for example:

go build -gcflags=-S fmt

prints the disassembly only for package fmt, while:

go build -gcflags=all=-S fmt

prints the disassembly for fmt and all its dependencies. last one in wins, no cap πŸ’…


🀝 more help, because we're thorough like that

  • For more about specifying packages, see go help packages.
  • For more about where binaries are installed, run go help gopath.
  • For more about calling between Go and C/C++, run go help c.
  • For more about project organization, run go help modules.
Note: go build adheres to certain conventions for organizing projects: it primarily supports go modules (see go help modules) while also supporting an alternative GOPATH mode (see go help gopath). Not all projects can follow these conventions, however. Installations that have their own conventions or that use a separate software build system may choose to use lower-level invocations such as go tool compile and go tool link to avoid some of the overheads and design decisions of the build tool. πŸ”§

πŸ‘€ SEE ALSO πŸ‘€

go install(1), go get(1), go clean(1).

like, go read those too. they're the supporting cast and they ate πŸ“


🫢 AUTHOR 🫢

This manual page was created using help2man and afterwards updating the output. It is maintained by the Debian Go Compiler Team <team+go-compiler@tracker.debian.org> for the Debian project (and may be used by others).

shoutout to the Debian Go Compiler Team, maintaining pages so we can all thrive 🌸


and that's the whole page bestie!! this was Go 1.27, dated August 2026, and if you flip to the bottom of the real thing it just says GO-BUILD(1) β€” go build, built different, periodt πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from