In this tutorial, we will learn about go run vs go build explained with example. Both go run and go build are really ways to build the go program. They differ in their function based on the need, requirement and use cases which we will deep dive in the next section.
What is Go Command
The ‘go’ command is a CLI (Command-line) tool used for various tasks in the Golang. It helps developers with tasks like building, running, testing and managing Go programs and modules. Go ships with many development tools and these are accessed via the ‘go’ command. There are two similar commands available via go. They are:
- go run
- go build
Each take either a single Go file, a list of Go files or the name of the package. Both commands serve specific purpose and differs in functionality. We will look into the difference between these two commands and their usage in the next section.
Go Run vs Go Build: Explained with Example
We will now understand the difference and usage of both the Go command along with an example to get more clarity. I am using below mentioned prerequisite as setup to execute the code used in this tutorial.
Also read: CPU Bound Processes in Golang [4 Best Examples]
Prerequisite
- Windows Operating System
- VS Code Installed
- Go package Installed
- Go plugin Configured in VS Code
What is Go Run?
Go run is go command used for quickly compiling and executing a Go source file without generating a persistent binary executable. This makes ‘go run’ command useful for testing out small programs or using Go like a scripting language. Go run command compiles the specified source file and immediately runs the resulting binary which is stored in temporary location. This temporary directory where binary is stored gets deleted as soon as the program finishes it’s execution. Let us look at an example where we will write a small go code and build it using ‘go run’ command.
Create a new file called test-command.go. Add below code to this file and save the file. Execute the file using ‘go run’ command as shown below.
package main import "fmt" func main() { fmt.Println("This file is executed via 'go run ' command\n") }
PS C:\Users\linuxnasa\OneDrive\Desktop\Go-Dump> go run .\test-command.go This file is executed via 'go run ' command
PS C:\Users\linuxnasa\OneDrive\Desktop\Go-Dump> ls Directory: C:\Users\linuxnasa\OneDrive\Desktop\Go-Dump Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---l 26-08-2023 23:20 84 test-command.go
What is Go Build?
Go build command is go command used to compile Go source code files into an executable binary. It generates a persistent binary that can be executed multiple times without recompilation. Go build command can be used when you want to create a binary that can be distributed, executed multiple times or integrated into larger projects. Let’ look at an example now where we will execute the same code as in example-1 and check the difference.
package main import "fmt" func main() { fmt.Println("This file is executed via 'go build '!!\n") }
PS C:\Users\linuxnasa\OneDrive\Desktop\Go-Dump> go build .\test-command.go
PS C:\Users\linuxnasa\OneDrive\Desktop\Go-Dump> ls Directory: C:\Users\linuxnasa\OneDrive\Desktop\Go-Dump Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 26-08-2023 23:36 1896960 test-command.exe -a---l 26-08-2023 23:35 109 test-command.go
Now the generated go binary can be executed on any platform which supports go. Execute the go binary to get the output as shown below.
PS C:\Users\linuxnasa\OneDrive\Desktop\Go-Dump> .\test-command.exe This file is executed via 'go build '!!
Summary
In this tutorial, we learnt about go run vs go build command. Go run is used for quick and temporary execution during the development while Go build is employed to create standalone, sharable executable for more polished and production scenarios.