Bizarre Panic on NewKey

54 views
Skip to first unread message

Robbie McKinstry

unread,
Aug 5, 2016, 12:26:11 AM8/5/16
to Google App Engine

Hey all,

TL; DR Can someone help explain why this code panics?

I'm an experienced Go developer trying to write a single entity to datastore in Go. Many of the tutorials are out of date (using the appengine.Context type instead of the net/context.Context type). For example, the official example code is out of date. However, once I updated the code, I had a weird and unintuitive panic from my call to NewKey(). For whatever reason, it appears that the call to google.golang.org/appengine/internal.fullyQualifiedAppID() panics. Well, I scrapped that code and tried a different tutorial.

I tried this tutorial and updated the code to use to correct context. I also scrapped all of the code that doesn't simply write to the database. That left me with this tiny skeleton here.
Still, oddly, I get the same bug! The exact same stack trace, except with a call to NewIncompleteKey() resulting in the panic instead of a call to NewKey() resulting in the panic.

I've spent too many hours trying to figure out this simple program, even digging through the app engine library's source code. Any help would be appreciated!

- Robbie McKinstry

Evan Jones

unread,
Aug 5, 2016, 9:56:38 AM8/5/16
to Google App Engine
I've actually only used Go to *query* the datastore, so I haven't tried creating keys. However, a quick note that might be related:


If you are using the "Standard Environment": you can't use App Engine APIs outside of handlers, because you need a "real" App Engine context. Make a web request to your instance, or use a "backend" instance and do your initialization as part of the /_ah/start request.

If you are using the "Flexible Environment": You may need to use appengine.BackgroundContext() instead of context.Background(). See the docs for that package.

Evan Jones

unread,
Aug 5, 2016, 10:40:17 AM8/5/16
to Google App Engine
I forgot to mention, the following Stack Overflow post is super useful on the subject of initialization with Go on App Engine: http://stackoverflow.com/questions/36184701/initializing-go-appengine-app-with-datastore

Robbie McKinstry

unread,
Aug 5, 2016, 12:45:29 PM8/5/16
to Google App Engine
Hey Evan, thanks for the reply! You saved my butt! I would have never figured this out on my own. I moved the call to save() out of the init function and into the warmup handler, and that did the trick!

Thanks again! :)
Reply all
Reply to author
Forward
0 new messages