如何正确导入 Golang 应用引擎? [英] How to correctly import Golang appengine?

查看:11
本文介绍了如何正确导入 Golang 应用引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我用 Go 编写的 Google App Engine 项目中,我一直在使用例如

In my Google App Engine project written in Go I've been using e.g.

import "appengine/datastore"

成功了很长时间,并假设导入找到了我拥有 App Engine SDK 的东西.但是,现在我想使用 Google 的第三方库,该库也使用 App Engine 中的内容,但使用完整路径导入:

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"

运行应用程序

$ goapp serve

没有找到 appengine 失败:

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

显然,我想使用相同的 App Engine 部件,以免出现其他问题.我的第一个想法是我想告诉第三方库使用 App Engine SDK 库,但我不知道如何,因为它在导入中有不同的前缀.

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.

我应该在我的项目中为所有 App Engine 导入使用完整路径吗?这与我在 Google 的 App Engine for Golang 网页上阅读的所有内容相反.例如.这里.

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.

一般来说,如何设置才能在 App Engine 的开发和生产以及第三方库中找到正确的 appengine 库?

提前感谢您的帮助!

更新

我还可以看到,在添加第三方库并运行 go get 后,它会将各种内容提取到 $GOPATH/src/google.golang.org/api/....那里有很多东西,还有 appengine 等等.似乎都是 Golang Google API...!

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...!

所以它没有解决,但我了解到有一个更改为 完全合格的 appengine 导入路径.

So it's not resolved but I have learned that there's a change that changes to fully qualified appengine import paths.

现在我的应用在本地运行,因为我引入了新的应用引擎以满足使用新导入路径的 oauth 库.

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

根据这个:

如果您不想更新整个应用以使用新的 App Engine 软件包,您可以同时使用两组软件包,仅使用带有 oauth2 软件包的新软件包.

实际上非常混乱,我不知道在 App Engine 上部署时有什么可用的.有人知道吗?

Actually very confusing and I don't know what is available when I deploy on App Engine. Does anyone know?

推荐答案

就像你的更新所说的那样;您可以同时使用两种类型的导入(在同一块代码中),同时它们正在弃用旧 API 并完成新 API:

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:

大多数 App Engine 服务都可以使用完全相同的 API.清理了一些 API,有些还没有.

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)

如果您需要同时使用 appenginegoogle.golang.org/appengine,那么您可以为导入路径设置别名以实现此目的.比如:

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.

如果在部署时某些内容不可用,您会在构建时遇到错误,并且不会部署到 App Engine,因此您不必担心.

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.

这篇关于如何正确导入 Golang 应用引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆