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.

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

报告相同问题?

悬赏问题

  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入
  • ¥15 关于液体厚度提取,和铺展轨迹数据化处理
  • ¥20 对方微信是wxid如何添加成好友?或者转换成微信号?
  • ¥15 yolov5实验数据好,但是自己拍摄的照片检测结果差
  • ¥15 做过dali2.0认证的技术,
  • ¥15 一道python难题
  • ¥15 matlab 免疫算法
  • ¥50 可视化界面构建,实现信息模块功能
  • ¥15 基于bert-base-chinese的中文文本处理
  • ¥15 高价求抖音抓包技术,抖音抓包无网络问题