为什么将 /tmp/go-build644681611/command-line-arguments/_obj/exe 传递给 go run?

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

Why is /tmp/go-build644681611/command-line-arguments/_obj/exe passed in to go run

问题

我正在使用beego框架学习和开发Go语言,但是我的hello world示例没有运行,我确定是因为beego根据os.Args[0]进行了chdir操作,程序在那里运行。这就是为什么应用程序可以运行,但找不到views目录的原因。

所以回溯到一个更简单的级别,文件名为'example.go':

package main

import (
	"fmt"
	"os"
)

func main() {
    fmt.Println(os.Getwd())
    fmt.Println(os.Args[0])
}

将输出:

/<运行'go run this_file.go'的目录>/ <nil>
/tmp/go-build178877254/command-line-arguments/_obj/exe/example

我对Go语言还不够了解,不明白为什么会这样。我首先想到的是我正在使用gvm(Go版本管理器),可能是因为某些设置不正确导致的。感谢任何帮助。

编辑:

显然,

  1. go build

  2. ./example

会产生更好、预期和成功的结果:

<运行'./example'的目录>/ <nil>
<运行'./example'的目录>/
英文:

I was playing and learning with the beego framework for go, but my hello world example didn't run and from what I determine is that beego does a chdir based on os.Args[0] and that the program is run there. And that is why the app runs, but can't find the views directory.

So backtracking to a simpler level, filename 'example.go':

package main

import (
	&quot;fmt&quot;
	&quot;os&quot;
)

func main() {
    fmt.Println(os.Getwd())
    fmt.Println(os.Args[0])
}

Will output:

/&lt;directory &#39;go run this_file.go&#39; was run in&gt;/ &lt;nil&gt;
/tmp/go-build178877254/command-line-arguments/_obj/exe/example

I don't understand enough about go as why this the way it is. My first thought is that I'm using gvm (go version manager) and this is happening because something is off. Any help appreciated.

EDIT:

Apparently,

  1. go build

  2. ./example

produces a better, expected, and successful outcome:

 &lt;directory &#39;./example&#39; was run in&gt;/ &lt;nil&gt;
 &lt;directory &#39;./example&#39; was run in&gt;/

答案1

得分: 4

go run将会把你的源代码编译成系统的TEMP或者TMP环境变量中的文件,然后从那里执行。它并不是真正为了运行生产代码而设计的,因为显然编译和执行的时间比直接执行要长。

通常的做法是在开发过程中使用go run来快速测试代码,然后使用go build来构建和分发二进制文件。go run实际上是为了方便开发者在一个命令中快速测试代码。它确实在在多个不同系统之间运行可移植源代码方面有一些有用的好处,但是如果你要将代码部署到所有这些系统上,通常在部署中包含go build是很正常的。请查看Go命令的完整指南

为了简化事情,你可以在一台机器上将Go代码交叉编译成多个目标架构,然后只需将二进制文件部署到相关的机器上。这意味着目标系统甚至不需要安装Go。除了上面的链接,你还可以查看goxc,它提供了一种更简单的方式,即单行交叉编译工具。

英文:

go run will compile (go build) your source into the system TEMP or TMP environment variable, then execute the file from there. It's not really designed for the running of production code, as it obviously takes longer to compile and execute than to just execute.

The usual practice is to use go run for development, and then go build and distribute the binaries. go run is really a convenience for the developer to quickly test code in one command. It does have some useful benefits in running portable source code between multiple, disparate systems, but, if you're going to deploy the code to all of these systems, it's usually normal to include the go build in the deployment. Check out the full guide to the Go command

To make matters easier, it's quite possible to just cross-compile Go code to multiple target architectures on one machine, then just deploy the binaries to the relevant machines. This means that the target systems don't even need Go installed. In addition to the link above, you can check out goxc which provides an even simpler way, single line, cross-compilation tool.

huangapple
  • 本文由 发表于 2013年6月10日 05:25:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/17014329.html
匿名

发表评论

匿名网友

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

确定