40

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 the go run FILE.go command in terms of performence etc.

Are there any advantages if I start a webservice with one of these methods?

2
  • 1
    possible duplicate of Go: How does go run file.go work
    – JimB
    Mar 5, 2015 at 15:37
  • 3
    Pass the -x flag to run, build, get or install to see what exactly is executed.
    – JimB
    Mar 5, 2015 at 15:38

3 Answers 3

61

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.

2
  • There 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.
    – 10 cls
    Nov 18, 2017 at 19:19
  • 1
    @10cls I didn't notice such behavior on my system. go run writes the executable in a temporary directory whereas go build writes it in the current directory. Maybe your temporary directory is a ramdisk? Mar 8, 2019 at 14:03
7

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.

6

'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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.