Basics
Go Running Code
Running Go Programs
Go programs run via go run or go build with .go files.
Running Go Programs with go run
The simplest way to run a Go program is by using the go run
command. This command compiles and executes the source code files directly. It's particularly useful for quick tests or running small scripts without creating a binary executable.
Building Executables with go build
For deploying applications, or if you need better performance, use the go build
command to compile your Go programs into executable binaries. This allows you to run the compiled program without needing to use the Go tooling again.
Understanding the Difference
- go run: Compiles and runs the Go program directly from the source file. Ideal for testing and development.
- go build: Compiles the source code into an executable. Suitable for production deployment as it generates a standalone binary.
Common Use Cases for go run and go build
The choice between go run
and go build
often depends on the development and deployment stage:
- Use
go run
during development for testing small scripts and functions. - Use
go build
when you need to distribute your application as a binary, ensuring it can run independently of the Go environment.
Basics
- Previous
- Installation
- Next
- Syntax