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 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办