go run: Usage, Options & Common CI Errors
Compile and run a Go main package in one step.
go run compiles a main package to a temporary location and runs it, then discards the binary. It is convenient for scripts and tools but not how you ship a binary.
What it does
Builds the named main package into a temp directory, executes it, and cleans up. Arguments after the package path are passed to the program, not to go.
Common usage
Terminal
go run . # run main in the current dir
go run ./cmd/server # run a specific main package
go run main.go # run a single file
go run . --port 8080 --verbose # pass program flagsCommon CI error: runs a non-main package
go run ./pkg fails with "go run: cannot run non-main package" because the target is a library. go run only works on package main. Fix: point it at the directory holding func main() (e.g. ./cmd/tool), or use go build / go test for library packages.
Options
| Arg | Effect |
|---|---|
| . | Main package in the current dir |
| ./cmd/x | A specific main package |
| <args> | Passed to the program |
Related guides
Go "build failed" - Read the First Compiler Error in CIFix a failing "go build" in CI -- a compile error, an undefined symbol, or a missing dependency. Read the fir…
go build: Usage, Options & Common CI Errorsgo build compiles Go packages and binaries. Learn -o, ./..., cross-compiling with GOOS/GOARCH, and how to fix…
go install: Usage, Options & Common CI Errorsgo install compiles and installs a binary into GOBIN. Learn the @version syntax for tools, where binaries lan…