duanchoupo1104 2013-06-09 21:25
浏览 140
已采纳

为什么/ tmp / go-build644681611 / command-line-arguments / _obj / exe传入运行

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,

  1. go build

  2. ./example

produces a better, expected, and successful outcome:

 <directory './example' was run in>/ <nil>
 <directory './example' was run in>/
  • 写回答

1条回答 默认 最新

  • douxun2018 2013-06-10 09:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)