Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang - Difference between "go run main.go" and compilation

After writing some scripts in Go I asked myself if there is any difference between the compilation of a .go-file and the later execution and the go run FILE.go command in terms of performence etc.

Are there any advantages if I start a webservice with one of these methods?

like image 426
user3147268 Avatar asked Mar 05 '15 15:03

user3147268


People also ask

Does go run compile?

The go run command compiles and runs a main package comprised of the .go files specified on the command line. The command is compiled to a temporary folder. The go build and go install examine the files in the directory to determine which .go files are included in the main package.

Do you need to compile golang?

Golang is a compiler-based language, it can easily be compiled on the development computer for any targeted system such as linux and mac. A golang project when have compiled turns to a self-sufficient executable and can be ran on the targeted system without anything additional.

What means go run?

When you run go run main.go , instead of checking the directory with a file that has a main function, the go program looks in the main.go file for that function and executes it just like go run . .

How is golang compiled?

Go is a compiled language. This means we must run our source code files through a compiler, which reads source code and generates a binary, or executable, file that is used to run the program. Examples of other popular compiled languages include C, C++, and Swift.


2 Answers

go run is just a shortcut for compiling then running in a single step. While it is useful for development you should generally build it and run the binary directly when using it in production.

like image 126
Innominate Avatar answered Oct 04 '22 15:10

Innominate


'go install' command will create shared library compiled file as package.a under pkg folder and exec file under bin directory.

go run command is useful while doing development as it just compiles and run it for you but won't produce binaries in pkg folder and src folder

like image 25
jagadeesh m Avatar answered Oct 04 '22 17:10

jagadeesh m