Build and run a Go program

In this lesson you’ll run a Go program with go run, turn it into a standalone program with go build, and create the go.mod file every Go project needs. You’ll also check your code with go fmt and go vet, build programs for other operating systems, and install your own tools with go install.

Before you start

You need Go installed; if go version doesn’t work in your terminal yet, follow Install Go first. I’ll also assume you can open a terminal, move into a folder with cd, and list its files (ls on macOS and Linux, dir on Windows).

Create a folder called hello and put the program from Hello, world in it, saved as hello.go:

package main

import "fmt"

func main() {
	fmt.Println("Hello, 世界")
}Code language: Go (go)
Hello, 世界Code language: plaintext (plaintext)

One command: go

Almost everything you do with Go goes through a single command, go, followed by a subcommand: go run, go build, go test and so on. Run go help to see the full list, or go help build for the details of one subcommand.

go run: compile and run in one step

In the hello folder, run:

go run hello.goCode language: Bash (bash)
Hello, 世界Code language: plaintext (plaintext)

Go is a compiled language, so go run does two things. It compiles your code into a program in a temporary folder, then runs that program. When the program ends, the temporary copy is thrown away. That’s perfect while you’re writing code: change something, go run again, see the result.

Make it a module: go mod init

Running a single file by name works, but real Go projects are modules: a folder with a go.mod file that names the project. Without one, most go commands refuse to work on the folder. Try building:

go buildCode language: Bash (bash)
go: go.mod file not found in current directory or any parent directory; see 'go help modules'Code language: plaintext (plaintext)

So let’s create one. go mod init takes one argument, the module path:

go mod init example.com/helloCode language: Bash (bash)
go: creating new go.mod: module example.com/hello
go: to add module requirements and sums:
	go mod tidyCode language: plaintext (plaintext)

(The hint about go mod tidy is for when you use other people’s packages. You can ignore it for now.) The folder now contains a go.mod file. On my machine it looks like this:

module example.com/hello

go 1.27.3Code language: plaintext (plaintext)

Two lines, that’s all a small program needs:

  • module example.com/hello is the module path, the name of your project. If you ever publish the code, it should be where it lives, like github.com/yourname/hello. For code that stays on your machine any name works; I use example.com/... because that domain is reserved for examples and will never clash with a real project.
  • The go line says which version of Go the module needs at minimum. Your version number may be different from mine. That’s fine.

Later, when your program uses packages written by others, go.mod also lists those dependencies and their versions. That, and splitting your own code into several packages, is covered in Packages and modules.

Inside a module you can refer to “the package in this folder” with a dot, so this is how I usually run a program:

go run .Code language: Bash (bash)
Hello, 世界Code language: plaintext (plaintext)

The difference from go run hello.go shows once your program has more than one file: go run . compiles all .go files in the folder together.

go build: make a program you can keep

When you want a program to keep, share or put on a server, use go build. It compiles your code just like go run, but it leaves the result in the current folder and doesn’t run it:

go build
lsCode language: Bash (bash)
go.mod
hello
hello.goCode language: plaintext (plaintext)

There’s a new file, hello. On Windows it’s called hello.exe. Go names it after the last part of the module path. To pick another name, use -o: go build -o greet.

What go build does with your source code The go tool compiles hello.go and go.mod into a program file called hello, or hello.exe on Windows. If the code has an error, the compiler stops with an error message and no program file is created. source hello.go go.mod your module folder go tool go build compiles and links program file hello hello.exe on Windows compile error ./hello.go:6:6: undefined: fmt.Prinln
go build turns your module into one program file, or stops with an error and builds nothing. go run does the same, then runs the program and deletes it.

If your code has a mistake, such as the fmt.Prinln typo from the previous lesson, go build prints the error and doesn’t create a program file at all:

# example.com/hello
./hello.go:6:6: undefined: fmt.PrinlnCode language: plaintext (plaintext)

Run the program

On macOS and Linux, run it with ./hello. The ./ means “the file in this folder”; without it, the shell only looks in the folders on your PATH.

./helloCode language: Bash (bash)
Hello, 世界Code language: plaintext (plaintext)

On Windows, run .\hello.exe in PowerShell (or just hello in Command Prompt).

What is this file?

The file is a binary (also called an executable): machine code for your operating system and processor. The file command on my Mac confirms it:

hello: Mach-O 64-bit executable arm64Code language: plaintext (plaintext)

It’s self-contained. Go’s runtime, including the garbage collector, is built into the file, so the people you give it to don’t need Go installed. They do need the same kind of system: a Mac binary won’t run on Windows. (You’ll fix that in a minute with cross-compiling.)

Because everything is included, even Hello, world is not tiny: my binary is about 2.4 MB. For most uses that doesn’t matter. If it does for you, read How to shrink a Go executable.

go fmt and go vet: tidy and check your code

go fmt rewrites the files in your package into the standard Go layout (tabs, spacing, braces) and prints the names of the files it changed. If your editor formats on save, it will rarely have anything to do.

go vet looks for code that compiles but is very likely wrong. A classic: fmt.Printf prints formatted text, and the %d in its first argument means “put a whole number here”. Here I pass it a piece of text instead:

package main

import "fmt"

func main() {
	name := "Gopher"
	fmt.Printf("Hello, %d!\n", name)
}Code language: Go (go)
go vetCode language: Bash (bash)
hello.go:7:21: fmt.Printf format %d has arg name of wrong type stringCode language: plaintext (plaintext)

The compiler is happy with this code; it builds and runs. But the output shows why vet complained:

Hello, %!d(string=Gopher)!Code language: plaintext (plaintext)

(The fix is %s, for strings. name := "Gopher" creates a variable; that’s the topic of the next lesson.) Run go vet now and then, especially before you share code. go test runs a subset of the vet checks automatically.

Cross-compiling: build for another system

Here’s one of my favorite Go features. You can build a program for a different operating system and processor from your own machine, with nothing extra to install. Two environment variables control it: GOOS (the operating system: windows, darwin for macOS, linux, …) and GOARCH (the processor: amd64, arm64, …).

On macOS or Linux, set them in front of the command:

GOOS=windows GOARCH=amd64 go build
GOOS=linux GOARCH=arm64 go build -o hello-linux-arm64
file hello.exe hello-linux-arm64Code language: Bash (bash)
hello.exe:         PE32+ executable (console) x86-64, for MS Windows
hello-linux-arm64: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, …Code language: plaintext (plaintext)

I built these on a Mac, and they run on a Windows PC and on an ARM Linux machine (such as a Raspberry Pi with a 64-bit OS). Notice that Go added .exe automatically for Windows. In PowerShell on Windows, the variables are set differently, and they stay set for the rest of that PowerShell session:

$env:GOOS = "linux"; $env:GOARCH = "amd64"; go buildCode language: Bash (bash)

To see every combination Go supports, run go tool dist list. This works out of the box for pure Go programs, which is everything in this tutorial. Programs that call C code through cgo need more setup to cross-compile.

go install: put your program on your PATH

go install builds your program like go build, but instead of leaving the binary in the current folder, it moves it into Go’s bin folder ($HOME/go/bin unless you set GOBIN):

go install
helloCode language: Bash (bash)
Hello, 世界Code language: plaintext (plaintext)

Running plain hello from any folder only works if that bin folder is on your PATH; Install Go shows how to add it. go install can also fetch and install a program someone else published, straight from its module path plus a version. For example, this installs the Go team’s own example program:

go install golang.org/x/example/hello@latestCode language: Bash (bash)

This is how most Go developer tools are installed. Note that it also installs a program called hello, replacing yours in the bin folder. Names matter there.

Practice

1. Create a new folder countdown, make it a module called example.com/countdown, and write a program that prints 3, 2, 1 and Liftoff! on separate lines. Build it with go build and run the binary.

Show a solution
mkdir countdown
cd countdown
go mod init example.com/countdownCode language: Bash (bash)

Save this as main.go (any name ending in .go works; main.go is the common choice):

package main

import "fmt"

func main() {
	fmt.Println(3)
	fmt.Println(2)
	fmt.Println(1)
	fmt.Println("Liftoff!")
}Code language: Go (go)
3
2
1
Liftoff!Code language: plaintext (plaintext)

Then go build and ./countdown (or .\countdown.exe on Windows).

2. Build the countdown program for Windows on ARM, using -o to call the file countdown-arm.exe.

Show a solution
GOOS=windows GOARCH=arm64 go build -o countdown-arm.exeCode language: Bash (bash)

With -o, Go uses exactly the name you give it, so include the .exe yourself.

Key takeaways

  • go run . compiles and runs your program, then throws the binary away.
  • go mod init example.com/name creates go.mod and turns a folder into a module.
  • go build creates a self-contained binary for your system; use -o to name it.
  • go fmt formats your code, go vet catches likely bugs the compiler allows.
  • Set GOOS and GOARCH to build for other systems; go install puts programs in Go’s bin folder.