user3517317
user3517317

Reputation: 379

Why is it recommended to use `go build` instead of `go run` when running a Go app in production?

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

Answers (1)

Adrian
Adrian

Reputation: 46452

Several reasons:

  • A Go application does not require the Go toolchain, just as a C application doesn't require 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

Related Questions