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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line