简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM