英文:
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版本管理器),可能是因为某些设置不正确导致的。感谢任何帮助。
编辑:
显然,
-
go build
-
./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 (
"fmt"
"os"
)
func main() {
fmt.Println(os.Getwd())
fmt.Println(os.Args[0])
}
Will output:
/<directory 'go run this_file.go' was run in>/ <nil>
/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,
-
go build
-
./example
produces a better, expected, and successful outcome:
<directory './example' was run in>/ <nil>
<directory './example' was run in>/
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论