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

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

问题描述

在我用 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 APIs...!

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.

现在我的应用程序在本地运行,因为我拉入了新的 appengine 以满足使用新导入路径的 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,有些 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 appengine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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