Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly import Golang appengine?

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?

like image 793
murrekatt Avatar asked Aug 28 '15 12:08

murrekatt


2 Answers

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.

like image 196
adam_0 Avatar answered Oct 13 '22 07:10

adam_0


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

like image 41
user1431972 Avatar answered Oct 13 '22 09:10

user1431972