Go: How does go run file.go work

Unlike in java, where the bytcode is created and interpreted at the execution time, go creates an executable file that is dependent on the machine being used,like in c, c++.


Command go run performs project's building under the hood (so yes it builds project)
and with flag --work (go run --work main.go) you can see the location of temporary build files.

Also in official documentation (go1.11) you can find:

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.


The go run command compiles and runs a main package comprised of the .go files specified on the command line. The command is compiled to a temporary folder.

The go build and go install examine the files in the directory to determine which .go files are included in the main package.


It's more or less the equivalent of running go build X.go -o /tmp/random-tmp-folder/exe && /tmp/random-tmp-folder/exe

Tags:

Go