英文:
Go - cannot find package "appengine"
问题
我已经按照这个指南安装了AppEngine SDK。
https://developers.google.com/appengine/docs/go/gettingstarted/introduction
我最初使用Brew在OSX上安装了Go 1.2。我设置了我的路径:
export GOPATH=$HOME/Documents/go
export PATH=$GOPATH/bin:$PATH
export PATH=$HOME/Documents/go/go_appengine:$PATH
我复制/粘贴了Hello World应用程序,并使用goapp serve
运行它。一切正常。
现在,当我尝试使用appengine
时:
import (
"appengine"
)
我得到了编译时错误:
api.go:5:5: 无法在以下任何位置找到包“appengine”:
/usr/local/Cellar/go/1.2/libexec/src/pkg/appengine(来自$GOROOT)
/Users/jan/Documents/go/src/appengine(来自$GOPATH)
起始指南文档没有提到这一点。似乎SDK有自己的$GOPATH
目录,包括/src
、/pkg
和/bin
。我猜我需要在SDK和本机Go之间手动切换$GOPATH
,这没有任何意义,而且对我来说也不起作用(因为我主要在非appengine的项目上工作)。
我显然在这里做错了什么。我漏掉了什么?
编辑:
似乎实际的appengine服务器编译和运行都很好,但我的整个设置都出问题了(测试,Vim...)。有没有什么解决方法?
英文:
I have followed this guide in installing the AppEngine SDK.
https://developers.google.com/appengine/docs/go/gettingstarted/introduction
I have originally installed Go 1.2 with Brew (on OSX). I've set my paths:
export GOPATH=$HOME/Documents/go
export PATH=$GOPATH/bin:$PATH
export PATH=$HOME/Documents/go/go_appengine:$PATH
I copy/pasted hello world app, and ran it with goapp serve
. All good.
Now, as soon as I try to use appengine
:
import (
"appengine"
)
I get compile time error:
api.go:5:5: cannot find package "appengine" in any of:
/usr/local/Cellar/go/1.2/libexec/src/pkg/appengine (from $GOROOT)
/Users/jan/Documents/go/src/appengine (from $GOPATH)
The starting guide documentation doesn't say anything about this. It seems like the SDK has its own $GOPATH
like dir with /src
, /pkg
and /bin
. I assume I would have to manually switch the $GOPATH
between SDK and native Go all the time, which doesn't make any sense and doesn't even work for me (as I mostly work on non-appengine stuff).
I am clearly doing something wrong here. What am I missing?
EDIT:
It seems like the actual appengine server is compiling and running fine, however my entire setup is broken (testing, Vim...). Is there any workaround?
答案1
得分: 7
根据alpe1提供的信息,以下代码行解决了vim编译器go的问题:
ln -s $APPENGINE_SDK/goroot/src/pkg/appengine $GOROOT/src/pkg/
ln -s $APPENGINE_SDK/goroot/src/pkg/appengine_internal $GOROOT/src/pkg/
mkdir -p $GOROOT/src/pkg/code.google.com/p/
ln -s $APPENGINE_SDK/goroot/src/pkg/code.google.com/p/goprotobuf
$GOROOT/src/pkg/code.google.com/p/
我需要更新gocode的lib-path(参见选项),以便为appengine实现自动完成:
gocode set lib-path "$APPENGINE_SDK/goroot/pkg/linux_amd64_appengine"
英文:
As provided by alpe1, the following lines solve the vim compiler go:
ln -s $APPENGINE_SDK/goroot/src/pkg/appengine $GOROOT/src/pkg/
ln -s $APPENGINE_SDK/goroot/src/pkg/appengine_internal $GOROOT/src/pkg/
mkdir -p $GOROOT/src/pkg/code.google.com/p/
ln -s $APPENGINE_SDK/goroot/src/pkg/code.google.com/p/goprotobuf
$GOROOT/src/pkg/code.google.com/p/
and I needed to update gocode lib-path (cf Options) to have the autocompletion for appengine:
gocode set lib-path "$APPENGINE_SDK/goroot/pkg/linux_amd64_appengine"
答案2
得分: 2
要测试appengine,请考虑使用"appengine/aetest"
,它替代了其他答案中提到的测试框架。
至于vim
,让我们避免在不同的GOROOT
目录之间创建符号链接。这是一种导致最严重和最糟糕的错误的方法:略微不匹配的库依赖关系。你考虑过在启动vim
之前导出不同的GOROOT
吗?你可以简单地将它放入别名中:
# 当然,你可以将这个放入你的 .bashrc、.bash_profile 或者 .zshrc 文件中
$ alias appvim="export GOROOT=$APPENGINE_SDK/goroot && vim"
syntastic
所做的就是在$GOROOT/src
中查找相关的包含文件。通过将$GOROOT
更改为appengine SDK的路径,你将检查正确的库。
英文:
For testing appengine, consider "appengine/aetest"
, which replaces the testing framework mentioned in other answers about this issue.
As for vim
, let's avoid symlinking between distinct GOROOT
directories. That's a recipe for bugs of the worst kind and sort: Slightly mismatched library dependencies. Have you considered simply exporting a different GOROOT
before you launch vim
? You could drop it into an alias trivially:
# You could of course drop this in your .bashrc, .bash_profile, or .zshrc
$ alias appvim="export GOROOT=$APPENGINE_SDK/goroot && vim"
All syntastic
does is look in $GOROOT/src
for the relevant includes. By changing $GOROOT
to be the appengine SDK's, you're going to check the correct libraries.
答案3
得分: 2
在包名前加上google.golang.org
,例如:
"google.golang.org/appengine"
适用于版本1.9.35。
英文:
Prefix the packages with google.golang.org
eg.
"google.golang.org/appengine"
Works with version 1.9.35.
答案4
得分: 1
你没有说你使用的Go App Engine SDK的版本。请确保它是从https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Go下载的最新版本。
你应该能够在提取的SDK上运行goapp serve
(或goapp build
,goapp test
等)而不需要进行任何更改。你只需要将go_appengine的路径添加到你的PATH
中。
你应该能够为常规Go和App Engine使用同一个GOPATH
。
你的错误信息暗示了GOROOT是/usr/local/Cellar/go/1.2/libexec
。当你调用goapp
时是这样吗?这不应该发生。如果你使用完整路径$HOME/Documents/go/go_appengine/goapp
会发生什么变化吗?
英文:
You don't say what version of the Go App Engine SDK you are using. Make sure it's the most recent from https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Go.
You should be able to run goapp serve
(or goapp build
, goapp test
, etc.) without any changes to the extracted SDK. You should only need to add the path to go_appengine to your PATH
.
You should be able to have a single GOPATH
for both regular Go and App Engine.
Your error message implies that the GOROOT is /usr/local/Cellar/go/1.2/libexec
. Is that when you invoke goapp
? That shouldn't happen. Does anything change if you use the full path $HOME/Documents/go/go_appengine/goapp
?
答案5
得分: 0
在Christopher的回答基础上,gocode
会在$GOROOT/pkg
目录中查找与你的架构匹配的路径(例如$GOROOT/pkg/darwin_amd64
)。然而,AppEngine的设置中的代码目录带有后缀_appengine
(例如$GOROOT/pkg/darwin_amd64_appengine
)。你可以通过创建符号链接来解决这个差异:
export APPENGINE_SDK=/your/appengine/sdk/directory/go_appengine
ln -s $APPENGINE_SDK/goroot/pkg/your_architecture_appengine $APPENGINE_SDK/goroot/pkg/your_architecture_amd64
请确保将上述的AppEngine目录和架构替换为你自己的。
完成后,创建一个别名来更改GOROOT并启动vim:
alias appvim="export GOROOT=$APPENGINE_SDK/goroot && vim"
如Christopher在他的评论中提到的那样。
英文:
Building on Christopher's answer, gocode
looks in $GOROOT/pkg
for a path matching your architecture (e.g. $GOROOT/pkg/darwin_amd64
). However, the code directory in AppEngine's setup is suffixed with _appengine (e.g. $GOROOT/pkg/darwin_amd64_appengine
). You can fix this discrepancy by creating a simlink:
export APPENGINE_SDK=/your/appengine/sdk/directory/go_appengine
ln -s $APPENGINE_SDK/goroot/pkg/your_architecture_appengine $APPENGINE_SDK/goroot/pkg/your_architecture_amd64
Be sure to replace the AppEngine directory and architectures above with your own.
After doing this, create an alias to change GOROOT and start vim
alias appvim="export GOROOT=$APPENGINE_SDK/goroot && vim"
as Christopher mentioned in his comment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论