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 π
-Cdir β 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 β¨-pn β the number of programs (like build commands or test binaries) that can be run in parallel. default isGOMAXPROCS, which is normally the number of CPUs you've got available π§-raceβ enable data race detection. supported only ondarwin/amd64,darwin/arm64,freebsd/amd64,linux/amd64,linux/arm64(only for 48-bit VMA),linux/ppc64le,linux/riscv64andwindows/amd64. check your platform first, don't get your hopes up π-msanβ enable interoperation with memory sanitizer. supported only onlinux/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 exceptlinux/amd64. so specific, so particular π«Ά-asanβ enable interoperation with address sanitizer. supported only onlinux/arm64,linux/amd64,linux/loong64. supported onlinux/amd64orlinux/arm64and only with GCC 7 and higher or Clang/LLVM 9 and higher. and supported onlinux/loong64only with Clang/LLVM 16 and higher. read that twice bestie π-coverβ enable code coverage instrumentation. we're watching π-covermodeset,count,atomicβ sets the mode for coverage analysis. default is βsetβ unless-raceis 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
-coverfor you π
-coverpkgpattern1,pattern2,pattern3β for a build that targets packagemain(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. peepgo help packagesfor 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 eachgo tool asminvocation.-buildmodemode β build mode to use. seego help buildmodefor 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=falseto always omit version control information, or-buildvcs=trueto 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 π-compilername β name of compiler to use, as inruntime.Compiler(gccgoorgc).-gccgoflags[pattern=]arg listβ arguments to pass on each gccgo compiler/linker invocation.-gcflags[pattern=]arg listβ arguments to pass on eachgo tool compileinvocation.-installsuffixsuffix β 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-raceflag, the install suffix is automatically set to race or, if set explicitly, has_raceappended to it. same deal for the-msanand-asanflags. using a-buildmodeoption that requires non-default compile flags has a similar effect. it just β¨knowsβ¨-jsonβ emit build output in JSON suitable for automated processing. seego help buildjsonfor the encoding details.-ldflags[pattern=]arg listβ arguments to pass on eachgo tool linkinvocation.-linksharedβ build code that will be linked against shared libraries previously created with-buildmode=shared. ms. linked-the-shared-libraries π-modmode β module download mode to use:readonly,vendor, ormod. by default, if a vendor directory is present and the go version ingo.modis 1.14 or higher, the go command acts as if-mod=vendorwere set. otherwise, the go command acts as if-mod=readonlywere 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 π¦-modfilefile β in module aware mode, read (and possibly write) an alternatego.modfile 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-modfileis specified, an alternatego.sumfile is also used: its path is derived from the-modfileflag by trimming the β.modβ extension and appending β.sumβ. sneaky but valid π-overlayfile β read a JSON config file that provides an overlay for build operations. the file is a JSON object with a single field, namedReplace, 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-overlayflag 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 throughgo runandgo testrespectively, and files beneathGOMODCACHEmay not be replaced. read the fine print bestie, fr fr π-pgofile β 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 π₯-pkgdirdir β install and load all packages from dir instead of the usual locations. for example, when building with a non-standard configuration, use-pkgdirto keep generated packages in a separate location. new home, same packages π-tagstag,listβ a comma-separated list of additional build tags to consider satisfied during the build. for more information about build tags, seego 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 βοΈ-toolexeccmd argsβ a program to use to invoke toolchain programs likevetandasm. for example, instead of runningasm, the go command will runcmd args /path/to/asm <arguments for asm>. theTOOLEXEC_IMPORTPATHenvironment variable will be set, matchinggo 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 buildadheres to certain conventions for organizing projects: it primarily supports go modules (seego help modules) while also supporting an alternative GOPATH mode (seego 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 asgo tool compileandgo tool linkto 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
- ubuntu stonking: golang-go 2:1.27~1 Β· Go
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms