2015-07-01 15:57
浏览 130

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 -
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?


图片转代码服务由CSDN问答提供 功能建议

我有一个使用gin-gonic定义的golang Web应用程序。 我已经在/ usr / local / goapp 下定义了goapp结构如下:-
/ usr / local / goapp
+ src
+ bin
+ pkg
GOPATH-/ usr / local
GOBIN-/ usr / local / goapp / bin
GOROOT-/ usr / local / go

当我在源代码的主文件夹下运行go build和go install时,我得到了我的二进制文件,并且能够运行它,并且看到我的html在进入主页URL时被加载。 如果我尝试在bin文件夹下运行相同的二进制文件,则在转到首页URL时看不到加载的html。 我找不到404页面。

我在这里缺少什么吗? 有人遇到过这种问题吗?


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题