英文:
How to correctly import Golang appengine?
问题
在我的使用Go编写的Google App Engine项目中,我一直使用以下方式导入包:
import "appengine/datastore"
并且一直以来都能成功定位到App Engine SDK中的相关内容。然而,现在我想使用来自Google的第三方库,该库也使用了App Engine的相关内容,但是导入路径是完整的:
import "google.golang.org/appengine"
当我运行应用程序时,出现了找不到appengine的错误:
...go/src/golang.org/x/oauth2/client_appengine.go:16: can't find import: "google.golang.org/appengine/urlfetch"
Can't find package "google.golang.org/appengine" in $GOPATH
显然,我希望继续使用App Engine的部分内容,以避免其他问题。我的第一个想法是告诉第三方库使用App Engine SDK的库,但是由于导入路径不同,我不知道该如何做。
在我的项目中,是否应该对所有App Engine的导入都使用完整路径?这与我在Google的App Engine for Golang网页上所读到的相反。例如,在这里。
总的来说,如何设置才能在开发和生产环境中正确找到appengine库,以及从第三方库中找到正确的库?
提前感谢您的帮助!
更新
我还注意到,在添加第三方库并运行go get
之后,它将各种内容都下载到了$GOPATH/src/google.golang.org/api/...
目录下。那里有很多东西,包括appengine等等。看起来这些都是Golang的Google API...!
所以问题还没有解决,但我了解到有一个更改,即完全限定的appengine导入路径。
现在,我已经在本地运行我的应用程序,因为我引入了新的appengine来满足使用新导入路径的oauth库。
go get google.golang.org/appengine
根据这里的说明:
如果您不想更新整个应用程序以使用新的App Engine包,可以并行使用两组包,只在oauth2包中使用新的包。
实际上非常令人困惑,我不知道在部署到App Engine时会有哪些可用的内容。有人知道吗?
英文:
In my Google App Engine project written in Go I've been using e.g.
import "appengine/datastore"
with success for a long time and assuming the import locates things where I have the App Engine SDK. However, now I want to use a third party library also from Google that uses things from App Engine as well, but imports with the full path:
import "google.golang.org/appengine"
Running the app
$ goapp serve
fails with not finding appengine:
...go/src/golang.org/x/oauth2/client_appengine.go:16: can't find import: "google.golang.org/appengine/urlfetch"
Can't find package "google.golang.org/appengine" in $GOPATH
Obviously I want to use the same App Engine parts to not have other problems. My first through is that I want to tell the third party library to use the App Engine SDK libraries, but I don't know how as it has a different prefix in the import.
Should I use the full path in my project for all App Engine imports? This would be opposite to what is in all what I have read on the Google's App Engine for Golang webpages. E.g. here.
Generally, what is the way to set up things so that it finds the right appengine libraries both in development and production on App Engine as well as from third party libraries?
Thanks in advance for any help!
UPDATE
I can also see that after adding the third party library and running go get
it fetched all kinds of stuff into $GOPATH/src/google.golang.org/api/...
. Lots of stuff there and appengine as well etc. It seems to be all Golang Google APIs...!
So it's not resolved but I have learned that there's a change that changes to fully qualified appengine import paths.
Now I have my app running locally as I pulled in the new appengine to satisfy the oauth library that uses the new import paths.
go get google.golang.org/appengine
According to this:
If you don't want to update your entire app to use the new App Engine packages, you may use both sets of packages in parallel, using only the new packages with the oauth2 package.
Actually very confusing and I don't know what is available when I deploy on App Engine. Does anyone know?
答案1
得分: 10
就像你的更新所说的那样,你可以在同一段代码中同时使用两种类型的导入,同时他们正在废弃旧的 API 并完成新的 API:
大多数 App Engine 服务都具有完全相同的 API。有一些 API 进行了清理,有一些尚不可用。
(来源:https://github.com/golang/appengine#user-content-3-update-code-using-deprecated-removed-or-modified-apis,从你提供的链接中稍微往下)
如果你需要同时使用 appengine
和 google.golang.org/appengine
,你可以给导入路径起别名来实现这一点。类似这样:
import (
oldAppengine "appengine"
"google.golang.org/appengine"
)
或者你可以使用你想要的任何名称。
如果在部署时某些内容不可用,构建时会出现错误,并且不会部署到 App Engine,所以你不必担心这个问题。
英文:
It's like your update says; you can use both types of imports in parallel (in the same chunk of code) while they're deprecating the old API and finishing up the new one:
> Most App Engine services are available with exactly the same API. A few APIs were cleaned up, and some are not available yet.
(source, a little further down from one of your links)
If you need to use both appengine
and google.golang.org/appengine
then you can alias the import paths to make this possible. Something like:
import (
oldAppengine "appengine"
"google.golang.org/appengine"
)
Or whatever you want to name them.
If something's not available when you deploy, you'll get errors on build and it won't deploy to App Engine, so you don't have to worry about it.
答案2
得分: 1
如果你正在使用gosdk,只需在与你的.go文件相同的目录中运行goapp get
,它将下载并安装依赖项到你的gosdk安装中。然后再次部署应用程序,应该可以编译无误。
当它工作时,没有提示,文件将被下载到gosdk\gopath\src目录下。
完成后会有一个可以忽略的警告信息:
go install: no install location for directory
C:\your_current_directory outside GOPATH
For more details see: go help gopath
英文:
If you are using gosdk, just run goapp get
in the same directory as your .go file and it will download and install the dependencies to your gosdk installation. You then deploy the app again and it should compile without problem.
When it's working there's no prompt and files will be downloaded to gosdk\gopath\src
After fininshing there will be a warning message which can be ignored:
> go install: no install location for directory
> C:\your_current_directory outside GOPATH
> For more details see: go help gopath
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论