Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use go run . to compile and run a Go program while you are developing it. Use go build when you want a separate executable file, and go install when you want to install a command so you can call it by name from your shell.

What you need to run a Go program

The official Go getting-started tutorial starts with installing Go, creating a simple program, and running it from its module directory. It lists some programming experience, a code editor, and a terminal as prerequisites. Follow the current installation instructions linked from that tutorial for your operating system.

Go source code is organized into packages, and packages are grouped into modules. A package intended to run as a command uses package main. The Go code guide explains this organization.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run a Go program with go run

From the directory containing your module, run:

go run .

The dot tells the Go command to use the package in the current directory. Go compiles the program and runs it, which makes this a convenient development workflow when you are making changes and want to see the result. It does not leave a binary executable in the directory.

If the program runs successfully, its output appears in the terminal. If compilation fails, Go reports the error there; fix the relevant source or configuration and run the command again.

Build a separate executable with go build

When you want an executable file that you can run separately, change to the application directory and run:

go build

The Go command compiles the packages and their dependencies. It does not install the result. For the simple application in its tutorial, the resulting executable is run from that directory like this:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Linux or macOS: ./hello
  • Windows: hello.exe

The filename shown here follows the tutorial’s example; executable naming and paths can differ with the project and operating system. The complete example is in Go’s compile and install tutorial.

Choose between go run, go build, and go install

Command Use it when Result
go run . You want to compile and run the current package during development. Runs the program; does not generate a binary executable in the project directory.
go build You want an executable file to run separately. Builds the command and its dependencies but does not install the result.
go install You want to install a command so it can be invoked by name from the shell. Places the installed command in Go’s install location; that directory needs to be on your shell path to invoke the command by name.

The Go command documentation describes a Go-specific tool for working with packages and commands, including downloading, building, installing, and testing them. It is not a general-purpose build system.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Install a command for use by name

Use go install when you want to run a Go command from outside its project directory without specifying the executable’s path. Go’s install location can be discovered or configured, and its exact value and shell-path setup depend on your platform and environment. The official tutorial shows platform-specific examples.

  1. Run go install for the command you want to install, following the tutorial’s example for your setup.
  2. Make sure Go’s install directory is included in your shell’s PATH.
  3. Open a shell where that path is active and invoke the command by its name.

If the shell cannot find the command, check that installation succeeded and that the install directory is present in the active shell’s PATH.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.