Golang - Difference between "go run main.go" and compilation
Solution 1
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.
Solution 2
For DEV (local) environment - use go run
,
For PROD environment - use go install
this one better than go build
because it installs packages and dependencies and you'll have Go toolchain.
Solution 3
'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
user3147268
Updated on March 15, 2020Comments
-
user3147268 over 4 years
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 thego run FILE.go
command in terms of performence etc.Are there any advantages if I start a webservice with one of these methods?
-
10 cls over 6 yearsThere must be more to it than that - if I time the println in a helloworld, it runs faster with go run than compiled. I noticed this on a more substantial program and see it's true all the way down to helloworld. I'd be interested to know why and at what point the results invert.
-
Nicolas Garnier over 5 years@10cls I didn't notice such behavior on my system.
go run
writes the executable in a temporary directory whereasgo build
writes it in the current directory. Maybe your temporary directory is a ramdisk?