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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集