How to Run a Go Program with go run
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
go run <package> [<arguments>]
| Part | What it does |
|---|---|
go run | Compiles 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
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.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/go/2026/go-run-main-go-code.webp)
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
go run .
Hello, world! This is go run.
Step 3: Pass an Argument
go run . Ada
Hello, Ada! This is go run.

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
| Mistake | What happens | Do this instead |
|---|---|---|
go run main.go in a program with several files | Only main.go is compiled, and code from the other files is missing | Run the package: go run . |
A file with package other than main | go run refuses: there's no program to run | Use package main with a func main |
Expecting the program to stay after go run | It's deleted when it ends | Use go build for a program you keep |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
no Go files in the folder | You're not in the folder with main.go | cd into it |
go.mod file not found in current directory or any parent directory | The folder isn't a module yet | go 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
- Compile and run Go program, the
go rundocumentation. - Tutorial: Get started with Go, which runs its first program with
go run ..
