We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

Go Run vs Go Build vs Go Install

Lane Wagner
Lane WagnerBoot.dev co-founder and backend engineer

Last published

Table of Contents

go run, go build, and go install compile Go packages for different purposes. go run executes a program without saving the binary in your working directory, go build produces an executable from a main package, and go install installs a compiled command on your machine.

All the content from our Boot.dev courses are available for free here on the blog. This one is the "Packages and Modules" chapter of Learn Go. If you want to try the far more immersive version of the course, do check it out!

Main Packages and Library Packages

Every Go program is made up of packages.

A package named main has an entry point at the main() function. A main package is compiled into an executable program.

A package by any other name is a "library package". Libraries have no entry point. Libraries simply export functionality that can be used by other packages.

Go Run

The go run command quickly compiles and runs a main package. The compiled binary is not saved in your working directory.

I typically use go run to do local testing, scripting, and debugging.

Conventionally, the file in the main package that contains the main() function is called main.go.

Say you have a simple hellogo module:

mkdir hellogo
cd hellogo
go mod init example.com/username/hellogo

Here's the main.go for the hellogo program:

package main

import "fmt"

func main() {
	fmt.Println("hello world")
}

Run the code with:

go run main.go

Go Build

The go build command compiles go code into a single, statically linked executable program. One of the beauties of Go is that you always go build for production, and because the output is a statically compiled binary, you can ship it to production or end users without them needing the Go toolchain installed.

Some new Go devs use go run on a server in production, which is a huge mistake.

Build the hellogo program with:

go build

Then run the new program:

./hellogo

Building a Library Package

Library packages have no main() function. Here's a small mystrings package as an example:

package mystrings

func Reverse(s string) string {
	result := ""
	for _, v := range s {
		result = string(v) + result
	}
	return result
}

Only capitalized names are exported, meaning they can be accessed by other packages. Uncapitalized names are private.

Because this isn't a main package, go build won't build an executable. However, go build will still compile the package and save it to the local build cache. It's useful for checking for compile errors.

Go Install

The go install command compiles and installs a package or packages on your local machine for your personal usage. When the package is a command, it installs the compiled binary in the GOBIN directory.

Inside the hellogo project, run:

go install

Go compiles and installs the hellogo program on your machine. You can then run it outside the project directory with:

hellogo

If you get an error regarding "hellogo not found", it means you probably don't have your Go environment set up properly. Specifically, go install is adding your binary to your GOBIN directory, but that may not be in your PATH.

Frequently Asked Questions

What does go run do?

The go run command quickly compiles and runs a main package. It is typically used for local testing, scripting, and debugging.

Does go run create an executable?

It compiles the main package, but the compiled binary is not saved in your working directory. Use go build when you want to keep the executable.

Does go build create an executable for every package?

No. It creates an executable for a main package. For a library package, go build compiles the package and saves it to the local build cache without creating an executable.

Should I use go run in production?

No. Build the program and ship the compiled binary to production instead of compiling it with go run on the server.

Where does go install put binaries?

The go install command puts the compiled binary in the GOBIN directory. That directory must be in PATH to run the command by name.