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?
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.
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.
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 . .
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.
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.
'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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With