当使用Go编译器的运行命令时,可执行文件会发生什么情况?

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

What happens to the executable when using the run command in the go compiler?

问题

使用go build myprogram.go命令会生成一个可执行文件,这在使用编译器时我很熟悉。

然而,使用go run myprogram.go命令不会创建任何可执行文件,但仍然会运行程序。

这是如何工作的呢?是先创建可执行文件然后再删除,还是像解释器一样直接运行代码?

英文:

Using the go build myprogam.go produces an executable file, which is what I'm familiar with when working with compilers.

However, using go run myprogram.go doesn't create any executable files and still runs the program.

How exactly does this work, is the executable created and then deleted or does it run the code like an interpreter would?

答案1

得分: 1

一个临时的可执行文件被创建。
对于go test也是一样的。

关于go test的文档

该包在一个临时目录中构建,以免干扰非测试安装。

来源

你也可以运行fmt.Println(os.Args[0])来查看正在创建的可执行文件。

英文:

A temporary executable is created.
The same is the case for go test.

Documentation on go test

The package is built in a temporary directory so it does not interfere with the non-test installation.

Source

You can also run fmt.Println(os.Args[0]) to see the executable file that is being created.

huangapple
  • 本文由 发表于 2016年11月5日 11:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/40434532.html
匿名

发表评论

匿名网友

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

确定