Go Build: From Source Code to Executable
go build is your factory for turning Go source into a runnable program. It compiles your packages and their dependencies into a single executable. Use it to create a binary for deployment, but don't confuse it with go install which puts the file in your.
Why it exists
Go was designed for simplicity and speed, including the development workflow. go build exists to provide a single, standardized command to transform source code into a distributable, executable binary, automating the complex process of dependency resolution, compilation, and linking that previously required custom scripts or Makefiles.
The mental model
go build is like a self-contained factory. You give it the blueprint (your .go source files), and it gathers all the raw materials (your code and its dependencies from go.mod), assembles them, and produces a finished product (a single executable file) at the factory door (your current directory). It doesn't put the product on a delivery truck (go install) or just test the assembly line (go run).
How it works
When you run go build, the Go toolchain analyzes the package in the current directory. It reads your go.mod file to identify and fetch all required dependencies. It then compiles your source files and the source files of all dependencies, linking them together into a single, statically-linked executable. The result is a file, typically named after the directory it was built in, that can run on the target operating system and architecture without needing Go or any external libraries installed. The build process is heavily cached, making subsequent builds much faster.
When to use it
Use go build when you need to create a distributable artifact. This is the standard command for CI/CD pipelines, for building binaries to copy into Docker images, or for compiling a command-line tool that you want to place in a specific directory manually.
When not to use it
For quick, temporary execution without creating a permanent binary, use go run. It compiles and runs in one step. To compile a tool and install it for system-wide use (placing it in your $GOBIN directory, which should be in your PATH), use go install.
One canonical example
Imagine a project in a directory named my-app with a main.go file inside. Running go build from within the my-app directory will produce an executable file named my-app (or my-app.exe on Windows) right in that same directory. You can then run ./my-app to execute it. To cross-compile for a different platform, like Linux from a Mac, you can use environment variables: GOOS=linux GOARCH=amd64 go build.
Interview question
For which scenario is go build the most suitable Go command?
- a.To analyze source code for potential errors without generating any output file.
- b.To create a standalone, distributable executable file for deployment.Correct
- c.To compile and immediately execute a program for temporary testing.
- d.To compile a program and automatically place it in your system's executable path.
Why? this is the answer
go build is specifically designed to produce a single, statically-linked executable for distribution or deployment, as detailed in the card's "When to use it" section. Options A and C describe the functions of go run and go install, respectively, while option A is not the primary purpose of go build.
Just read this? Test yourself on what you have been reading.
Read the original → pkg.go.dev
- #go
- #build tools
- #compilation
- #cli
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on go — each one lists the topics its interview covers.
See open roles