LinuxCommandLibrary

go-run

Compile and execute Go programs

TLDR

Run a Go file

$ go run [path/to/file.go]
copy

Run a main Go package
$ go run [path/to/package]
copy

SYNOPSIS

go run [build flags] package [arguments...]

PARAMETERS

build flags
    Flags to pass to the go build command during the compilation phase. Common flags include -gcflags (compiler flags), -ldflags (linker flags), and -tags (build tags). See go build -help for a complete list.

package
    Specifies the package containing the main function to be executed. This can be a path to a directory containing the Go source files, a single Go source file, or a standard package name (e.g., 'fmt'). If no main function exist or defined, it will fail and error message will be printed.

arguments...
    Arguments passed to the compiled Go program. These arguments are available to the program through the os.Args variable.

DESCRIPTION

The go run command is a convenient way to compile and execute Go programs directly from source code files. It eliminates the need for separate compilation and linking steps, making it ideal for rapid prototyping, testing, and running small Go utilities. Behind the scenes, go run builds an executable in a temporary directory and then executes it. The executable is removed after it finishes running. go run is particularly useful when you want to quickly execute a single Go file or a small collection of files without the overhead of managing a separate build process. It automatically resolves dependencies and handles the compilation process, allowing you to focus on writing and running your code. If multiple .go files are passed it assumes there's a 'main' package inside and will compile the main package. Also, standard output from the compiled program is inherited by the user's terminal.

CAVEATS

The executable created by go run is stored in a temporary directory and is deleted after execution. Consequently, you cannot easily reuse or redistribute the compiled program. If you need a persistent executable, use go build instead.

Package path should be resolvable by the go toolchain. Relative paths like ./main.go or ../mypackage are acceptable. The path can also be a URL for fetching and running from a remote location.

REMOTE EXECUTION

go run can execute programs directly from remote repositories by providing a URL as the package argument.

Example: go run github.com/user/repo/cmd/program

This fetches the code, compiles it, and runs it, all in one step.

BUILD TAGS

You can use build tags to conditionally compile parts of your code. Use the -tags flag to specify build tags when running your program.

Example: go run -tags=debug main.go

HISTORY

The go run command has been part of the Go toolchain since its early releases. It was introduced to simplify the process of running Go programs during development and testing. Over time, it has been refined and optimized to provide a seamless experience for executing Go code directly from source. The command reflects the emphasis in Go's philosophy for ease of use, speed of compilation and overall simplicity.

SEE ALSO

go build(1), go install(1), go clean(1)

Copied to clipboard