go-run
Compile and execute Go programs
TLDR
Run a Go file
Run a main Go package
SYNOPSIS
go run [build flags] [packages | .go files] [arguments...]
build flags: Optional flags that control the compilation process, similar to those used with go build.
packages | .go files: Specifies the Go source code to run. This can be a package path (e.g., ".") or a list of individual Go source files (e.g., "main.go other.go").
arguments...: Any arguments that follow the package or file specification are passed directly to the executed Go program, not to the go run command itself.
PARAMETERS
packages | .go files
The Go source code to compile and run. This can be a package path (e.g., `.` for the current directory) or a list of `.go` files (e.g., `main.go`). It specifies what to run.
arguments...
Any arguments that follow the package or file specification are passed directly to the Go program being executed, not to the go run command itself. These are your program's command-line arguments.
-race
Enable data race detection during compilation and runtime. This flag helps identify concurrent access issues.
-tags 'tag,list'
A comma-separated list of build tags to consider satisfied during the build. Used for conditional compilation.
-v
Print the names of packages as they are compiled and linked. Useful for verbose output during the build process.
-x
Print the commands `go run` executes (e.g., the underlying `go tool compile`, `go tool link`, and the final execution command). Highly useful for debugging build issues.
-ldflags 'flags'
Arguments to pass to the linker. Often used to embed build information (e.g., version strings, commit hashes) into the final executable.
-gcflags 'flags'
Arguments to pass to the Go compiler. Allows fine-tuning compiler behavior for specific needs.
-mod mode
Sets the module download mode. Accepted modes include `readonly` (default), `vendor` (use modules in `vendor` directory), or `mod` (allow network operations to fetch modules).
-trimpath
Remove all file system paths from the compiled executable. This helps create reproducible builds and smaller binaries.
DESCRIPTION
The go run command is an essential part of the Go programming language's toolchain, designed for rapid development and testing. It provides a convenient way to compile and execute Go source code without explicitly managing the intermediate executable file. When invoked, go run first compiles the specified Go packages or individual .go files into a temporary executable. After successful compilation, it immediately runs this temporary executable. Once the program finishes execution, the temporary binary is automatically deleted. This command is particularly useful for quickly trying out code snippets, running scripts, or testing application logic during the development phase. Unlike go build, which creates a persistent executable file, go run abstracts away the compilation step, making it feel more like an interpreted language for rapid prototyping. Arguments provided after the Go source files or package path are passed directly to the executed Go program.
CAVEATS
- Temporary Executable: go run creates a temporary executable file, typically in the Go build cache or a system temporary directory, and deletes it upon completion. This means you cannot easily inspect the compiled binary or share it directly after running.
- Development Tool: While convenient, go run is primarily intended for development and testing. For deploying applications, it's generally recommended to use go build to create a named, persistent executable that you can then manage and distribute.
- Performance Overhead: For very large projects, the compilation step, though fast for Go, still adds overhead compared to directly executing an already built binary. Repeatedly running go run might recompile if source files change, which is expected but worth noting.
<I>HOW IT WORKS</I>
When you execute go run, the Go toolchain performs two main steps:
1. Compilation: It compiles the specified Go source files and their dependencies into an executable binary. This temporary binary is placed in a build cache or a temporary directory.
2. Execution: Immediately after successful compilation, the temporary executable is launched. Any arguments provided after the Go source files are passed directly to this running program.
Once the program finishes, the temporary executable file is removed. This seamless compile-then-run process makes go run highly convenient for iterative development.
HISTORY
The go run command has been an integral part of the Go toolchain since the early versions of Go (Go 1.x). It was designed to simplify the development workflow by abstracting the explicit compilation and execution steps into a single command. This design choice reflects Go's emphasis on developer productivity and ease of use, allowing for a more script-like feel when interacting with Go programs during development. Its core functionality has remained consistent, adapting to changes in the Go module system and build cache mechanisms over time.