Go 1.8 + datastore.RunInTransaction

211 views
Skip to first unread message

aleem....@gmail.com

unread,
Jul 25, 2017, 12:41:32 PM7/25/17
to google-appengine-go
I just opted into the go 1.8 beta for appengine standard because a library I am depending on has moved to using the native context.Context.

However, it seems that the appengine datastore package does not support the native context.Context and instead still uses golang.org/x/net/context

How do I use the datastore and memcache packages on GAE Go 1.8 standard?


thanks!

mg...@shift.com

unread,
Jul 26, 2017, 2:14:13 PM7/26/17
to google-appengine-go
Since the old and new "Context" interfaces are equivalent you can pass them back and forth interchangeably. Do you have an example that wasn't working for you?

aleem....@gmail.com

unread,
Jul 26, 2017, 3:08:13 PM7/26/17
to google-appengine-go
Here's the error I get when running go build:

./DAO.go:125: cannot use func literal (type func("context".Context) error) as type func("golang.org/x/net/context".Context) error in argument to "google.golang.org/appengine/datastore".RunInTransaction

Here's a subset of my code:


func MyFunction(c context.Context, f func(tc context.Context) (bool, error, *Build), opts *datastore.TransactionOptions) error {
       
// do some stuff
 err := datastore.RunInTransaction(c, func(tc context.Context) error {

 
var innerError error
 didSaveBuild
, innerError, build = f(tc)
 
return innerError
 
}, opts)
       
// do more
 
return err

}


The bolded line throws the error...

mg...@shift.com

unread,
Jul 27, 2017, 1:51:23 PM7/27/17
to google-appengine-go
When you're passing a function as a parameter, Go requires the function signature to match exactly. RunInTransaction requires a "func(x/net/context.Context)" type, so you need to change the signature of the function you're passing there.

However, you can then pass "tc" as a parameter to functions that expect a "context.Context", since their interfaces are compatible.

Aleem Mawani

unread,
Jul 27, 2017, 4:10:44 PM7/27/17
to mg...@shift.com, google-appengine-go
Thanks for that, I'll give that a shot.

--
You received this message because you are subscribed to a topic in the Google Groups "google-appengine-go" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-appengine-go/G_aq-B2x3-w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-appengin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine-go/7801ad48-5f70-477c-aed9-f5b0d60b202c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages