Skip to main content

How to Run a Go Program with go run

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

go run compiles a Go program and runs it in one step, without leaving a program file behind. It's the quickest way to try code while you write it, and anything you type after the package reaches the program as arguments.

Syntax​

Syntax
go run <package> [<arguments>]
PartWhat it does
go runCompiles the program into a temporary file and runs it.
<package>What to run. . means the package in the folder you're in, which must be package main with a func main.
<arguments>Optional. Words passed to the program, which it reads from os.Args.

The Example​

The example program greets the name it receives as an argument, or the whole world when it receives none.

Prerequisites​

  • Go. Check it with go version. This guide uses Go 1.27.4.

Step 1: Create the Module and the Program​

Terminal
mkdir hello-run
cd hello-run
go mod init example.com/hello-run

How to Create a Go Module with go mod init explains the last command. Then create main.go:

main.go, which sets name to world, replaces it with os.Args[1] when an argument is given, and prints Hello with the name and This is go run.

main.go
package main

import (
"fmt"
"os"
)

func main() {
name := "world"
if len(os.Args) > 1 {
name = os.Args[1]
}
fmt.Printf("Hello, %s! This is %s.\n", name, "go run")
}

os.Args holds the command line: the program's own path first, then the arguments. os.Args[1] is therefore the first argument, and the if only reads it when there is one.

Step 2: Run It​

Terminal
go run .
Output
Hello, world! This is go run.

Step 3: Pass an Argument​

Terminal
go run . Ada
Output
Hello, Ada! This is go run.

A zsh terminal where go run . prints Hello, world! This is go run. and go run . Ada prints Hello, Ada!

Everything after the package goes to the program, not to go. The commands are the same in zsh, bash and PowerShell.

go run or go build?​

go run builds into a temporary folder and deletes the program when it ends, which suits trying code out. go build writes the program into the folder you're in, so you can run it again without compiling, or copy it to another computer of the same kind.

Common Mistakes​

MistakeWhat happensDo this instead
go run main.go in a program with several filesOnly main.go is compiled, and code from the other files is missingRun the package: go run .
A file with package other than maingo run refuses: there's no program to runUse package main with a func main
Expecting the program to stay after go runIt's deleted when it endsUse go build for a program you keep

Troubleshooting​

SymptomLikely causeFix
no Go files in the folderYou're not in the folder with main.gocd into it
go.mod file not found in current directory or any parent directoryThe folder isn't a module yetgo mod init <module-path>

FAQ​

Is go run slower than running a built program?​

It compiles first, so the first run takes a moment. Go caches the compiled packages, so the next runs start faster.

Can I run a program from another folder?​

Yes. Give its package path relative to where you are, such as go run ./tools/hello, as long as you're inside the module.

Conclusion​

go run . compiles the program in the current folder and runs it, and the words after the . reach the program in os.Args, starting at os.Args[1].

Additional Resources​