I have a go server which I normally run like this:
go build . && ./main
But online I see many examples using go run . which is better to use and what is the difference?
I have a go server which I normally run like this:
go build . && ./main
But online I see many examples using go run . which is better to use and what is the difference?
From official documentation (go1.11
):
go run
- compiles and runs the named main Go package.
go build
- compiles the packages named by the import paths,
along with their dependencies, but it does not install the results.
go install
- compiles and installs the packages named by the import paths.
It means:
Usually for LOCAL environment it's ok to use go run
,
but for PROD environment it's better to build your app with go build
and run ./main
,
but in case you need Go toolchain you must use go install
because it installs packages and dependencies and run ./bin/main
(it may make sense in dev/stage environment).