What is the difference between Go run, Go build and Go install commands?

difference between Go run, Go build and Go install commands's picture

Go is a statically typed, compiled programming language designed by Robert Griesemer, Rob Pike, and Ken Thompson. Usually referred to as Golang but the actual name is Go, the two reasons for the name is Go was designed at Google and secondly was because the first URL was golang.org. It is syntactically identical to C, but with the plus points of memory safety, better garbage collection, structural typing, and CSP(Communicating sequential processes) - style execution of out-of-order or in partisan order.

go run, go build and go inst .]

all are three somewhat similar commands. These three commands either work on a single Go file or a list of Go files. Usually, these commands run on command lines for macOS/Linux its terminal and for windows its command prompt.

Here is the sample file which might be helpful for you to understand the commands better.

1 2 3 4 5 6 7 // file name : demo.go package main import "fmt" func main(){ fmt.Println("Demo Program!!") }

go run

It compiles and executes the main package which contains the .go file specified on the terminal. This command compiles the statement and stores it in a temporary file. The syntax for the command is -

go run filename. go

If you run this in a command line you’ll get the output from the main function’s print statement but the compilation won’t be stored anywhere. As the code complies from high-level language to low language and stores in a temporary file, using go run is useful and highly recommended when you’re learning the Go or working/testing a small programme or on the testing phase of your application and don’t want to take the storage.

For example, if you run the command using the sample -

go run demo.go

The output in the command line will be -

Demo Program

go build

go build scrutinizes the files in the directory to see which file (from the list) or part of the file is actually included in the main package. It is usually used to compile the packages and dependencies that are user-defined i.e. defined by you or pre-defined ones but you used in your file. When you have the binary file you made for the application or part of the bigger project to use later or it is a file that can be used in other applications or want to excess this file remotely then go build is the command for you. Using this command helps you build a different and permanent binary file in the current directory of your project/application. The syntax is -

go build filename. go

go build only focuses on building the packages and never on the result so it does discard the result once you exit the window.

The command using the above example will look like -

go build demo.go

go install

go install exactly works like go build but the only difference is instead of purring the binary file/ the complied file in the current directory (or user-specified directory) it places it in the ‘$GOPATH/bin’ directory with other binary files and packages of third-party tools that you installed via go get. It builds and afterwards also installs the packages into the ‘$GOPATH/bin’ unlike go build. The syntax is similar -

go install filename. go

The difference is that the output that you wanted to show using the print statement in your main package will be shown when you run ‘$GOPATH/bin’ in your command line.

If for trial purposes you want to use the same code as above, the go install will be written like this -

go install demo. go

You Should also know read about Top 6 Golang Backend Framework For Developer

GOARCH and GOOS

GOARCH and GOOS are Go’s cross-platform build commands. If you are working and developing an application on one kind of operating system and want to transfer and compile the file to the other kind GOARCH and GOOOS are the commands for you.

Below is an example of compiling a golang binary for Linux on a macOS terminal:

GOOS=linux GOARCH=amd64 go build

GOOS and GOARCH can build the source code without the help of a compiler.

Wrap up

This article is about helping you to understand go run, go build, and go install better and also enlightens the fact that the commands might seem similar but at the backend, they work differently, and all three have their own purpose in the context of go development.

Build Your Golang Team