Mastering Go's Core Commands: run, build, install, and get
Olivia Novak
Dev Intern · Leapcell

Go's simplicity and efficiency are not just in its language design but also in its powerful and intuitive command-line tools. For any Go developer, a solid understanding of go run
, go build
, go install
, and go get
is fundamental. These commands form the backbone of the Go development workflow, from rapid prototyping to deployment and dependency management. Let's explore each of them in detail, accompanied by practical examples.
1. go run
: The Quick Prototyper
The go run
command is arguably the most frequently used command for Go developers, especially during the early stages of development and for quick scripts. It compiles and runs the specified Go source files without producing an executable file on disk in the current directory. This makes it incredibly convenient for rapid iteration and testing.
How it works:
When you execute go run
, Go internally compiles the source code into a temporary executable file and then executes it. Upon completion, this temporary executable is removed.
Syntax:
go run [build flags] [packages]
Common Use Cases:
- Running simple scripts: For quick one-off tasks or testing small code snippets.
- Rapid prototyping: When you want to see the immediate output of your code changes without the intermediate step of building a binary.
Example 1: Running a Single File
Let's say you have a file named hello.go
:
// hello.go package main import "fmt" func main() { fmt.Println("Hello Go World!") }
To run this file, simply execute:
go run hello.go
Output:
Hello Go World!
Example 2: Running Multiple Files in a Package
If your main
package is split across multiple files, go run
can handle that too.
Suppose you have main.go
and util.go
in the same directory:
// main.go package main import "fmt" func main() { fmt.Println("Result of calculation:", calculateSum(5, 3)) }
// util.go package main func calculateSum(a, b int) int { return a + b }
To run this program:
go run main.go util.go
Or, more idiomatically, to run the main package in the current directory:
go run .
Output:
Result of calculation: 8
Important Note: While go run
is excellent for development, it's not suitable for deployment scenarios because it doesn't produce a persistent executable. For deployment, go build
or go install
are the appropriate choices.
2. go build
: Compiling for Distribution
The go build
command is used to compile Go packages and dependencies. Unlike go run
, it creates an executable binary file (or a package archive) that can be distributed and run independently.
How it works:
go build
compiles the Go source code. If the package is a main
package, it produces an executable. If it's a library package, it typically caches the compiled package archive (e.g., .a
files) in the build cache (typically GOCACHE
), which speeds up subsequent builds.
Syntax:
go build [build flags] [packages]
Common Use Cases:
- Creating executable binaries: For deploying applications.
- Cross-compilation: Generating binaries for different operating systems and architectures.
- Testing compilation: To ensure your code compiles without actually running it.
Example 1: Building a Simple Executable
Using our hello.go
from before:
// hello.go package main import "fmt" func main() { fmt.Println("Hello Go World!") }
To build an executable:
go build hello.go
This will create an executable file named hello
(on Linux/macOS) or hello.exe
(on Windows) in the current directory. You can then run it directly:
./hello
Output:
Hello Go World!
Example 2: Building with Output Name (-o
)
You can specify the name of the output executable using the -o
flag:
go build -o myapp hello.go
Now, the executable is named myapp
.
./myapp
Output:
Hello Go World!
Example 3: Cross-Compilation
One of Go's most powerful features is its built-in support for cross-compilation. You can easily build binaries for different target platforms (OS and architecture) by setting the GOOS
and GOARCH
environment variables.
To build hello.go
for Linux on an ARM 64-bit architecture (e.g., a Raspberry Pi) from a macOS machine:
GOOS=linux GOARCH=arm64 go build -o hello_linux_arm64 hello.go
This will produce a hello_linux_arm64
executable that can be run on a Linux ARM64 system.
Building a Module:
When working within a Go module, go build
will build the package in the current directory by default if no arguments are provided.
# In your module root, assuming main.go exists go build
This builds the main package in the current directory and places the executable in the current directory.
3. go install
: Installing Binaries and Packages
The go install
command is similar to go build
but with a crucial difference: it places the compiled executable or package archive in a standard location in your Go environment, specifically $GOPATH/bin
(if $GOBIN
is not set) or the path specified by the GOBIN
environment variable. This makes it ideal for installing command-line tools or libraries that you want to be available globally in your system's PATH.
How it works:
go install
first compiles the package. If it's a main
package, it then moves the resulting executable binary to the GOBIN
directory. If it's a library package, it installs the compiled package archive into the pkg
directory within your Go module cache.
Syntax:
go install [build flags] [packages]
Common Use Cases:
- Installing command-line tools: When you want to use a Go program as a system command (e.g.,
golangci-lint
,delve
). - Managing global utilities: Keeping various Go-based tools readily accessible.
- Building and caching dependencies: For library packages,
go install
builds and caches them for faster future builds.
Example 1: Installing a Command-Line Tool
Let's create a simple tool greeter.go
:
// greeter.go package main import ( "fmt" "os" ) func main() { if len(os.Args) < 2 { fmt.Println("Usage: greeter <name>") return } name := os.Args[1] fmt.Printf("Greetings, %s!\n", name) }
To install this tool:
go install greeter.go
After installation, the greeter
executable will be in your $GOPATH/bin
or $GOBIN
directory. Ensure this directory is in your system's PATH
.
Now, you can run it from anywhere in your terminal:
greeter Alice
Output:
Greetings, Alice!
Example 2: Installing from a Remote Repository (Go Modules)
With Go Modules, go install
is the preferred way to install an executable from an external repository directly into your GOBIN
. This bypasses the need to clone the repository manually.
go install github.com/spf13/cobra@latest
This command will fetch the cobra
module, compile its main
package, and place the executable (likely named cobra
) in your GOBIN
directory. The @latest
suffix tells Go to fetch the latest stable version. You can also specify a specific version tag (e.g., @v1.2.3
).
Difference between go build
and go install
:
go build
puts the executable in the current directory (or specified by-o
).go install
puts the executable in$GOBIN
(or$GOPATH/bin
).go install
also installs compiled packages to the build cache, making subsequentgo build
commands faster if they depend on those packages.
4. go get
: Managing Dependencies (Legacy and Modern)
The go get
command has evolved significantly with the introduction of Go Modules.
Pre-Go Modules (GOPATH
mode):
In the old GOPATH
mode, go get
was primarily used to download and install packages from remote repositories into your $GOPATH/src
directory. It also managed dependencies by downloading them recursively.
With Go Modules (Modern Approach):
With Go Modules (introduced in Go 1.11, default since Go 1.16), the role of go get
has changed. Its primary function is now to:
- Add, upgrade, or downgrade dependencies in your
go.mod
file. - Synchronize your
go.mod
andgo.sum
files.
How it works (Go Modules):
When you run go get
within a module, it updates your go.mod
and go.sum
files to reflect the desired dependency versions. It then fetches the necessary modules into your module cache (usually $GOPATH/pkg/mod
). Subsequent go build
, go run
, or go test
commands will automatically use these cached dependencies.
Syntax:
go get [build flags] [packages]
Common Use Cases (Go Modules):
- Adding a new dependency: When you want to use a new external package.
- Upgrading dependencies: To get the latest compatible versions of your existing packages.
- Downgrading dependencies: To revert to an older version.
- Cleaning up unused dependencies: Although this is typically handled by
go mod tidy
.
Example 1: Adding a New Dependency
Let's assume you have a Go module initialized:
mkdir mymodule cd mymodule go mod init mymodule
Now, let's create a main.go
that uses an external library, say rsc.io/quote
:
// main.go package main import ( "fmt" "rsc.io/quote" // This dependency is not yet in go.mod ) func main() { fmt.Println(quote.Go()) }
If you try to go run main.go
or go build
, Go will detect the missing dependency and prompt you to go mod tidy
or automatically try to go get
it.
To explicitly add it (and fetch it):
go get rsc.io/quote
After this command, your go.mod
file will be updated, looking something like this:
module mymodule go 1.22 require rsc.io/quote v1.5.0 // or a similar version
And a go.sum
file will be created/updated with cryptographic checksums.
Now you can run your program:
go run main.go
Output:
Don't communicate by passing memory, share memory by communicating.
Example 2: Upgrading a Dependency
To upgrade an existing dependency to its latest compatible version:
go get rsc.io/quote@latest
This will update the go.mod
and go.sum
files to point to the most recent stable release.
Example 3: Downgrading or Specifying a Version
If you need a specific version:
go get rsc.io/quote@v1.5.2
Note on go get
and go install
:
As mentioned earlier, go install
is now the preferred way to "get and install" a command-line tool. While go get
could previously do this (especially in GOPATH
mode), go install
is less ambiguous and better integrated with the module system for tool installation.
For example, to install delve
(Go debugger):
go install github.com/go-delve/delve/cmd/dlv@latest
This is generally preferred over go get github.com/go-delve/delve/cmd/dlv
.
Conclusion
The go run
, go build
, go install
, and go get
commands are the pillars of the Go development toolkit.
- Use
go run
for quick execution and development iteration. - Use
go build
to create distributable executables for your applications. - Use
go install
to install Go programs as system-wide tools or to cache library packages. - Use
go get
(within Go Modules) to manage your project's dependencies, ensuring correct versions and reproducible builds.
Mastering these commands allows Go developers to efficiently manage their projects, from writing the first line of code to deploying robust applications. They embody Go's philosophy of simplicity and efficiency, providing powerful functionality through clear and concise commands.