dongxilan9351 2015-07-01 15:57
浏览 184
已采纳

golang二进制文件无法在gopath src文件夹之外工作

I have a golang web application defined using gin-gonic. I have defined the the goapp under /usr/local/goapp The structure is like this -
/usr/local/goapp
+src
+bin
+pkg
Here are my go env -
GOPATH - /usr/local
GOBIN - /usr/local/goapp/bin
GOROOT - /usr/local/go

When I run go build and go install under the main folder in source, I get my binary and I am able to run it and see my html getting loaded when I go to the home page URL. If I try to run the same binary under bin folder, I don't see the html getting loaded when I go to the home page URL. I am getting 404 page not found.

Am I missing something here? Has anyone came across such a kind of issue?

Thanks.

  • 写回答

1条回答 默认 最新

  • duanjingwei7239 2015-07-01 16:05
    关注

    All those GOPATH, GOROOT and of course the missing PATH variables just say something about how to call the go program itself and where it searches for modules. But you told us, that you built and installed some go program.

    When a go program has been built go is actually not needed anymore. You can take the binary, put it anywhere you want, even to another machine, that has the same or at least a similar system and run that program there.

    When your program, that you have built and installed and that seems to be called gin-gonic is executed, you will execute it in some path, also known as the current working directory (see getcwd(2) or pwd(1)).

    I just guess now, that under the current working directory there lives your htdocs, index.html, whatever files, that this gin-gonic uses to create the pages.

    It is common, that, if such a program cannot find the document it should create, send, produce, whatever, it will return the code 404: Not found.

    Though just guessed, this is, quite likely, the situation you are in, when you run your program with a different current working directory, than the working directory, under which the program expects its documents.

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

报告相同问题?

悬赏问题

  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器
  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)
  • ¥15 交叉注意力机制的残差问题
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?