Go包/文件位置

huangapple go评论79阅读模式
英文:

Go Package/File Locations

问题

从Go文档的外观来看,它们似乎在暗示你必须将你正在工作的代码放在$GOPATH下,这是正确的吗?

我想在我的~/Documents目录下设置一个测试项目的文件夹,但每次我运行go install example/newmath(就像在测试示例中一样),它都会显示以下内容:

λ MacBook-Air src → go install example/newmath
警告:设置的GOPATH为GOROOT(/usr/local/go)无效
无法加载包:包example/newmath:导入“example/newmath”:找不到包

这是否意味着我需要重置我的$GOPATH/$GOROOT?我迷失了。

英文:

From the looks of the go documentation, they make it seem like you have to put code you're working on under your $GOPATH-- is that correct?

I would like to set up a test project in a dir under my ~/Documents, but everytime i run go install example/newmath (like in the test example) it says the following--

λ MacBook-Air src → go install example/newmath
warning: GOPATH set to GOROOT (/usr/local/go) has no effect
can't load package: package example/newmath: import "example/newmath": cannot find package

Does that mean that i need to reset my $GOPATH/$GOROOT? I'm lost.

答案1

得分: 4

在一般情况下,不需要设置GOROOT。然而,GOPATH应该被设置(并且导出)。

我建议从以下命令开始:

   export GOPATH=$HOME

然后将一个具有import "whatever"的包放入$GOPATH/src/whatever

当你熟悉Go构建系统(go {build, install})的工作方式后,你可以根据自己的需求来调整GOPATH。一个人甚至可以在$GOPATH中有多个项目,但我认为这在开始阶段并不重要,有时甚至永远不需要。

英文:

No need to setup GOROOT in the common situation. GOPATH, OTOH, should be set (and exported).

I would recommend to start with

   export GOPATH=$HOME

Then just put a package having import "whatever" into $GOPATH/src/whatever.

You can refine your GOPATH to your needs later, when you get used to how things work with the Go build system (go {build, install}). One can have even multiple items in $GOPATH, but I really don't think it's a concern in the beginning, and sometimes never.

huangapple
  • 本文由 发表于 2013年3月19日 05:13:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/15487052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定