Reputation: 379
I have a Dockerfile that I used to compile the Go app using go build
. I researched and indeed go build
is recommended for production.
However I cannot find a proper answer as to why.
I understand go run
creates a temporary file that and that cleans up on app termination.
But if I am using Docker, why is it bad to use go run
instead of go build
?
Upvotes: 8
Views: 11973
Reputation: 46452
Several reasons:
gcc
. If you use go build
, you can just deploy a binary; if you use go run
, you have to install the toolchain.go run
compiles the application unnecessarily every time it's run, increasing startup times.go run
forks the application to a new process, making process management unnecessarily complicated by obscuring PIDs and exit statuses.go run
has the potential to unexpectedly absorb code changes when you're only intending to run an application. Using go build
only when you want a fresh binary allows you to re-run the same, consistent binary every time with no unexpected changes.go run
ignores *.syso files while go build
recognizes and links them.Upvotes: 19