38

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?

user3147268
  • 1,814
  • 7
  • 26
  • 39
  • 1
    possible duplicate of [Go: How does go run file.go work](http://stackoverflow.com/questions/28755916/go-how-does-go-run-file-go-work) – JimB Mar 05 '15 at 15:37
  • 3
    Pass the `-x` flag to `run`, `build`, `get` or `install` to see what exactly is executed. – JimB Mar 05 '15 at 15:38

3 Answers3

60

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.

Innominate
  • 2,643
  • 2
  • 17
  • 8
  • 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 '17 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? – Nicolas Garnier Mar 08 '19 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.

cn007b
  • 16,596
  • 7
  • 59
  • 74
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

jagadeesh m
  • 73
  • 2
  • 9