Go:go run file.go 的工作原理是什么?

huangapple go评论94阅读模式
英文:

Go: How does go run file.go work

问题

go run 命令既不编译也不解释文件,它会编译并运行指定的 Go 源文件。当你使用 go run 命令时,它会在后台自动执行以下两个步骤:

  1. 首先,它会将指定的源文件编译为临时的可执行文件。
  2. 然后,它会立即运行该可执行文件。

这意味着 go run 命令会在运行之前自动编译源文件,并且不会在磁盘上生成任何可执行文件。希望这能帮到你!

英文:

The commands go build and go install compile the files into binaries. Does go run compile or interpret the file? I couldn't find explanations online and may have missed it. Appreciate pointers. Thanks!

答案1

得分: 45

这句话的中文翻译是:这基本上相当于运行go build X.go -o /tmp/random-tmp-folder/exe && /tmp/random-tmp-folder/exe

英文:

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

答案2

得分: 15

go run命令编译并运行由命令行指定的.go文件组成的主包。该命令会被编译到一个临时文件夹中。

go buildgo install命令会检查目录中的文件,以确定哪些.go文件包含在主包中。

英文:

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.

答案3

得分: 13

命令go run在幕后执行项目的构建(是的,它构建项目)。
并且通过使用--work标志(go run --work main.go),您可以查看临时构建文件的位置。

此外,在官方文档(go1.11)中,您可以找到以下信息:

go run - 编译并运行指定的主要Go包。

go build - 编译由导入路径指定的包及其依赖项,但不安装结果。

go install - 编译并安装由导入路径指定的包。

英文:

Command go run performs project's building under the hood (so yes it builds project)
<br>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.

答案4

得分: 5

与Java不同,Go语言在执行时不会创建和解释字节码,而是创建一个依赖于所使用机器的可执行文件,类似于C、C++。

英文:

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++.

huangapple
  • 本文由 发表于 2015年2月27日 09:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/28755916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定