Building and Running Programs

A short guide to turning Go source into a runnable program with go build, go run, and go install, and the three pages that explain each command in depth

Overview

Every Go program begins as plain text in .go files. Before it can do anything useful, those files must be turned into a binary the operating system can execute. The go command provides three entry points for that transformation.

Go is a compiled language. The compiler translates Go source into machine code for a specific operating system and architecture, and the linker packages everything into a single, statically linked binary. That binary contains your code plus the library code it depends on, so no separate runtime or interpreter is needed on the target machine.

All three commands share the same compilation engine. They differ in what happens after compilation, whether the result is saved as a named file, executed immediately from a temporary location, or copied into a central bin directory.

A beginner can hold this simple picture in mind.

  • go build - "Turn my source into a runnable file I can keep and share."
  • go run - "Just show me what my code does, right now."
  • go install - "Put the finished program somewhere my terminal can always find it."

All three expect a package that declares package main and contains a func main(). Without those, there is no entry point for an executable. Building a library package only checks that the code compiles.

What This Group Covers

The three pages under this heading each cover one command in depth, with flags, cross-compilation, and the mistakes beginners make most often.

Compiling with go build

go build compiles the named packages and, for a main package, writes an executable binary to the current directory. This page explains binary naming, the -o flag, linker flags for stamping version information into a binary, cross-compilation, and build tags.

Running Programs with go run

go run compiles a main package and immediately executes the resulting binary, then cleans up the temporary file. This page explains what it accepts as an argument, how program arguments and build flags are separated, and when it is the wrong tool for the job.

Installing Binaries with go install

go install compiles a package and places the resulting executable in the Go binary directory, so the program can be run from any terminal by name. This page explains GOBIN, installing remote tools with @version, and cross-compiled installs.

Choosing Between the Three

None of the three commands is better in general. Each solves one stage of the development lifecycle.

SituationCommandWhy
I want to see my code run right nowgo run .Fast feedback loop, no leftover binary
I need a binary to test on another machinego build -o myappPortable artifact in one file
I am creating a tool I will use from anywherego installBinary lands in PATH, accessible globally
I am building for a different OSGOOS=... go buildCross-compilation produces a target-specific binary
I am developing a shared librarygo build with no binary expectedChecks that the package compiles

A beginner often sticks with go run for everything because it feels like an interpreter. As soon as the work needs to be shared with a colleague or deployed, switch to go build. When a tool should stay available across projects, reach for go install.

go build

Learn how to compile Go programs into standalone executables with go build, including cross-compilation, build flags, and build constraints

go run - Compile and Run Go Programs in One Step

Learn how go run compiles and executes Go source files in a single command, how it handles arguments, and when to use it instead of go build

go install - Installing Binaries and Versioned Tools

Learn how go install compiles Go source code and places the resulting executable into a central location, how to configure the installation target