Go Run vs Go Build vs Go Install: What’s the Difference and When to Use Each?

BhuwanBhuwan
5 min read

If you're just starting to program in Go, you may be wondering what the difference is between go run, go build, and go install. These are all essential tools in the Go development toolkit - and while they may look similar at first glance, each one has its own unique purpose.

In this article, we'll explain what these commands do, when to use them, and how they fit into your Go development workflow.

go run — For Quick Execution

The go run command is the fastest way to compile and run a Go program in a single step - so it's great for prototyping, testing, and small utilities that don't need to be built as standalone binaries.

For Go developers, it's like a “just run it and see what happens” tool.

What Exactly Does go run Do?

Temporarily compile the code as a binary behind the scenes.

Run the temporary binary immediately.

No binary is saved after execution - once the program is executed, the compiled file is deleted.

Therefore, every time you use go run, Go recompiles the code before running.

When to Use go run

go run is great for

  • Performing quick experiments or tests

  • Trying out new syntax or language features

  • Writing and running small scripts

  • Debugging individual files or logic blocks

  • Explore third-party packages in standalone segments.

Example

If you have a simple program in main.go, you can run it like this:

go run main.go

If your program is spread across multiple files (e.g. main.go and utils.go), you can specify them together:

go run main.go utils.go

You can even use it for remote URL files (since Go 1.16+):

go run https://raw.githubusercontent.com/<youruser/example>/main/hello.go

📌 Note: This only works for publicly accessible files and requires the Go module to be enabled.

go build — For Compiling Your Program

The go build command is used to compile Go code into a binary executable without executing it. This command is especially useful when you want to test the way your code is compiled, build a separate binary file for distribution, or prepare to produce a ready-to-run executable.

Unlike go run, which compiles and executes immediately, go build compiles your program and leaves a binary file ready for you to run.

What does go build actually do?

Compiles Go code into a separate executable binary file.

By default, the resulting binary is placed in your current working directory.

Does not install the binary globally or modify your system PATH.

Compiles all required source files and packages, including dependencies.

When to use go build

Compiling projects for local testing

Generating binary files to share or move to other systems

Creating build files for different platforms (using cross-compilation)

Test that the code has been compiled without actually running the program

Example Usage

Let’s say you have a Go project with a main.go file.

go build

This will create an executable in your current directory. If your folder is called myapp, you’ll get a binary named:

./myapp

To specify a custom name for the binary:

go build -o myprogram main.go

Then run it like this:

./myprogram

If your project imports external packages, go build will automatically fetch and compile these dependencies if they are not already on your system. When you use Go modules (i.e., you have go.mod files), it also references go.mod and go.sum files to accurately parse and lock down dependent versions to ensure consistency and repeatability between builds.

go install — For System-Wide Installation

The go install command is used to compile Go code and install the resulting binary file to a global location on the system so that it can be run from any terminal window. This is ideal when you want to build command-line tools or reusable applications that you can easily call just like the built-in system commands.

Unlike go build, which creates a binary file in your local project folder, go install places the compiled executable in the directory specified by the environment variable $GOBIN, which is preset to $GOPATH/bin if you don't have $GOBIN set.

What it does:

Compiles the Go program, just like go build.

Saves the binary to the global binary directory ($GOBIN or $GOPATH/bin).

No binary will be created in your working directory.

Works with both local packages and remote module paths.

When to use go install :

Want to make your Go program globally accessible as a command-line tool.

Work with multiple projects or teams and need a consistent local install.

Are installing third-party tools such as golangci-lint, delve, or your own developer utilities.

Need to simplify usage - no need to browse project folders or manually execute binaries.

Examples

To install a local Go project:

go install

After running this command, your compiled binary is placed in your system’s Go bin directory. If that directory is in your system’s PATH, you can run your tool from anywhere:

myapp

To install a tool from a remote source (e.g., GitHub):

go install github.com/<yourusername/toolname>@latest

This will download the module, compile it, and place the binary in $GOBIN, ready to be used from the terminal globally.

If you use Go a lot to build command-line tools - even for internal use - go install is your best friend. It lets you treat tools like any other system command and run them instantly from anywhere in the terminal.

1
Subscribe to my newsletter

Read articles from Bhuwan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bhuwan
Bhuwan