英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论