I'm migrating my Golang AppEngine app to 1.12+, and I need to switch to cloud.google.com/go/datastore
. It's not clear to me how to use it with AppEngine, could someone please verify my assumptions?
My assumption is that somewhere inside main()
I can run (note the context.Background()
):
db, err := datastore.NewClient(context.Background(), datastore.DetectProjectID)
if err != nil {
panic(err)
}
defer db.Close()
And then from my handlers I can use that db:
func blah(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
key := datastore.NameKey("blah", blah, nil)
db.Get(ctx, key, blah2)
}
Am I correct? Or do I need to run datastore.NewClient()
separately from each web handler?