英文:
Where does GO look for google-cloud-sdk ? What should GOPATH be?
问题
我在osX上设置Go App引擎遇到了问题。google-cloud-sdk路径应该放在GOROOT还是GOPATH中?
我将google-cloud-sdk放在了/usr/local目录下。
看起来在goroot/目录下有源代码:
/usr/local/google-cloud-sdk/platform/google_appengine/goroot/
go env
GOPATH="/usr/local/google-cloud-sdk/platform/google_appengine/goroot"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
$ go get
package google.golang.org/appengine: 无法下载,/usr/local/google-cloud-sdk/platform/google_appengine/goroot是GOROOT,而不是GOPATH。更多详情请参阅:'go help gopath'
package google.golang.org/appengine/datastore: 无法下载,/usr/local/google-cloud-sdk/platform/google_appengine/goroot是GOROOT,而不是GOPATH。更多详情请参阅:'go help gopath'
当我尝试更改PATH以包含/src时:
GOPATH="/usr/local/google-cloud-sdk/platform/google_appengine/goroot/src"
$ go get
package google.golang.org/appengine: mkdir /usr/local/google-cloud-sdk/platform/google_appengine/goroot/src/src: 权限被拒绝
package google.golang.org/appengine/datastore: 无法在任何位置找到包"google.golang.org/appengine/datastore":
/usr/local/go/src/google.golang.org/appengine/datastore(来自$GOROOT)
/usr/local/google-cloud-sdk/platform/google_appengine/goroot/src/src/google.golang.org/appengine/datastore(来自$GOPATH)
我将google-cloud-sdk的路径追加到GOROOT中:
export GOROOT="/usr/local/go/:/usr/local/google-cloud-sdk/platform/google_appengine/goroot"
GO似乎不喜欢在GOROOT中有多个路径:
$ go get
go: 无法找到GOROOT目录:/usr/local/go/:/usr/local/google-cloud-sdk/platform/google_appengine/goroot
我在将源代码复制到/usr/local后运行了./install.sh脚本。
添加的额外路径没有修复我遇到的错误。
我看到了这个答案:https://stackoverflow.com/questions/11286534/test-cases-for-go-and-appengine
但这是5年前的答案,看起来有些笨拙/不可靠。在5年的时间里,应该有更优雅的解决方案,而不是复制单个目录和创建符号链接。
编辑**********************
mv /usr/local/google-cloud-sdk/ ~/go/
然后从.bash_profile中删除了GOPATH和GOROOT
然后我运行了./install.sh
我尝试运行'go get':
$ go get
go install: 目录/Users/Bryan/work/gocode/skincarereview在GOPATH之外没有安装位置
由于失败了,我将代码的工作目录路径添加到了GOPATH,并将google-cloud-sdk的路径追加到了PATH中:
export GOPATH="/Users/Bryan/work/gocode/skincarereview"
export PATH=$HOME/google-cloud-sdk:$PATH
go get
以相同的消息失败:
$ go get
go install: 目录/Users/Bryan/work/gocode/skincarereview在GOPATH之外没有安装位置
更多详情请参阅:'go help gopath'
英文:
I'm having trouble setting up Go App engine on osX. Should the google-cloud-sdk path be in GOROOT or GOPATH?
I put the google-cloud-sdk in /usr/local
It looks like there is source code in: goroot/
/usr/local/google-cloud-sdk/platform/google_appengine/goroot/
go env
GOPATH="/usr/local/google-cloud-sdk/platform/google_appengine/goroot"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
$ go get
package google.golang.org/appengine: cannot download, /usr/local/google-cloud-sdk/platform/google_appengine/goroot is a GOROOT, not a GOPATH. For more details see: 'go help gopath'
package google.golang.org/appengine/datastore: cannot download, /usr/local/google-cloud-sdk/platform/google_appengine/goroot is a GOROOT, not a GOPATH. For more details see: 'go help gopath'
When I attempted to change the PATH to include /src:
GOPATH="/usr/local/google-cloud-sdk/platform/google_appengine/goroot/src"
$ go get
package google.golang.org/appengine: mkdir /usr/local/google-cloud-sdk/platform/google_appengine/goroot/src/src: permission denied
package google.golang.org/appengine/datastore: cannot find package "google.golang.org/appengine/datastore" in any of:
/usr/local/go/src/google.golang.org/appengine/datastore (from $GOROOT)
/usr/local/google-cloud-sdk/platform/google_appengine/goroot/src/src/google.golang.org/appengine/datastore (from $GOPATH)
I appended the path to google-cloud-sdk to GOROOT:
export GOROOT="/usr/local/go/:/usr/local/google-cloud-sdk/platform/google_appengine/goroot"
GO doesn't seem to like multiple paths in GOROOT:
$ go get
go: cannot find GOROOT directory: /usr/local/go/:/usr/local/google-cloud-sdk/platform/google_appengine/goroot
I did run the ./install.sh script after I copied the source to /usr/local
The additional PATH's added did not fix the errors I was having.
I saw this answer: https://stackoverflow.com/questions/11286534/test-cases-for-go-and-appengine
But it's from 5 years ago and it seems clunky/hacky. It would seem in 5 years there would be a more elegant solution that copying individual directories and creating symlinks.
EDIT **********************
mv /usr/local/google-cloud-sdk/ ~/go/
then deleted GOPATH and GOROOT from .bash_profile
I then ran ./install.sh
I attempted to run 'go get':
$ go get
go install: no install location for directory /Users/Bryan/work/gocode/skincarereview outside GOPATH
Since that failed, I added the path to the working directory of code AND appended the path to google-cloud-sdk to PATH:
export GOPATH = "/Users/Bryan/work/gocode/skincarereview"
export PATH=$HOME/google-cloud-sdk:$PATH
go get
get failed with the same message:
$ go get
go install: no install location for directory /Users/Bryan/work/gocode/skincarereview outside GOPATH
For more details see: 'go help gopath'
答案1
得分: 7
它既不放在$GOROOT也不放在$GOPATH中。只需将其解压到您的$HOME目录并运行安装程序。如果需要,通过将以下行添加到您的.bash_profile
文件中将其添加到您的$PATH中。
export PATH=$HOME/google-cloud-sdk:$PATH
确保您还使用gcloud components install app-engine-go
命令获取了golang SDK。https://cloud.google.com/appengine/docs/standard/go/download
请勿更改您的路径以包含google-cloud-sdk/platform/google_appengine/goroot/src
目录。那样会导致问题。您应该将$GOPATH保持为正常安装。使用App Engine SDK for Go会自动使用该目录中的源代码,无需进行任何操作。
此外,除非您打算从源代码编译新的Go版本(即新的语言版本),否则永远不要手动更改$GOROOT。它会自动为您设置正确的$GOROOT。https://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really
如果您的安装出了问题(我曾经遇到过这种情况),只需删除云SDK以及$PATH中与之相关的任何引用。还要完全卸载常规的Go安装。然后从头开始。安装Go,解压google-cloud-sdk,运行安装程序(如果需要,将其添加到$PATH),gcloud components install app-engine-go
。完成。
在开发App Engine时,您的Go源代码应放在常规的$GOPATH中,而不是放在google-cloud-sdk/...
的任何位置。要在本地运行dev_appserver,请运行dev_appserver.py [path-to-source]
,其中给定的路径包含您的代码和app.yaml文件。我通常会进入我的项目路径(例如cd $HOME/go/src/myproject
)并使用dev_appserver.py ./
运行。https://cloud.google.com/appengine/docs/standard/go/tools/using-local-server
部署的详细步骤在此处有说明。https://cloud.google.com/appengine/docs/standard/go/tools/uploadinganapp
编辑:文件夹结构。
$GOPATH = $HOME/go
google-cloud-sdk文件夹的位置
英文:
It goes in neither $GOROOT or $GOPATH. Just unpack it to your $HOME directory and run the installer. If necessary, add it to your $PATH by adding this line to your .bash_profile
.
export PATH=$HOME/google-cloud-sdk:$PATH
Make sure you grab the golang SDK as well with gcloud components install app-engine-go
https://cloud.google.com/appengine/docs/standard/go/download
DO NOT change your path to include the src dir in google-cloud-sdk/platform/google_appengine/goroot/src
. That will break things. You leave your $GOPATH to be your normal installation. Using the App Engine SDK for Go automatically uses the sources in that dir without any manipulation.
Also, you should NEVER MANUALLY change $GOROOT unless you plan on compiling a new Go version from source (as in a new version of the language). It will automatically set the proper $GOROOT for you. https://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really
If your install is messed up beyond reason (happened to me once), just remove the cloud SDK and any references to it in your $PATH. Also completely uninstall the regular Go installation. Then start from scratch. Install Go, unpack google-cloud-sdk, run installer (add to $PATH if needed), gcloud components install app-engine-go
. Voila.
When developing for App Engine, your go sources go into your REGULAR $GOPATH. They DO NOT go in google-cloud-sdk/...
anywhere. To run the dev_appserver locally, run dev_appserver.py [path-to-source]
where the given path contains your code and the app.yaml. I usually cd
in to my project path (e.g. cd $HOME/go/src/myproject
) and run with dev_appserver.py ./
. https://cloud.google.com/appengine/docs/standard/go/tools/using-local-server
Deployment is covered here. https://cloud.google.com/appengine/docs/standard/go/tools/uploadinganapp
EDIT: Folder structure.
$GOPATH = $HOME/go
Location for google-cloud-sdk folder
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论