title: How could I manage the App Engine Go runtime context to avoid App Engine lock-in?
body: <p>I'm writing a Go application to run on App Engine's Go runtime.</p>
  
  <p>I notice that pretty much any operation which uses an App Engine service (such as Datastore, Mail, or even Capabilities) requires that you pass it an instance of <code>appengine.Context</code> which must be retrieved using the function <code>appengine.NewContext(req *http.Request) Context</code>.</p>
  
  <p>While I am writing this app for App Engine, I want to be able to move it to some other platform (possibly one which doesn't support any of the App Engine API) easily and quickly if I should so choose.</p>
  
  <p>So, I'm abstracting away the actual interaction with App Engine services and API's by writing little wrappers around any App-Engine-specific interaction (including request handling functions).  With this approach, if I ever do wish to move to a different platform, I'll just rewrite those specific modules which tie my application to App Engine.  Easy and straightforward.</p>
  
  <p>The only problem is that <code>appengine.Context</code> object.  I can't pass it down from my request handlers through my layers of logic to the modules which handle these API's without tying pretty much all of my code to App Engine.  I could pass the <code>http.Request</code> object from which the <code>appengine.Context</code> object can be derived, but that would require coupling things that probably shouldn't be coupled.  (I think it's best practice for none of my application to even know it's a web application except those portions specifically dedicated to handling HTTP requests.)</p>
  
  <p>The first solution that sprang to mind was to just create a persistent variable in some module.  Something like this:</p>
  
  <pre><code>package context
  
  import (
      "appengine"
  )
  
  var Context appengine.Context
  </code></pre>
  
  <p>Then, in my request handlers, I can set that variable with <code>context.Context = appengine.NewContext(r)</code> and in the modules that directly use App Engine services, I can fetch the context by accesing <code>context.Context</code>.  None of the intervening code would need to know of the <code>appengine.Context</code> object's existence.  The only problem is that <a href="https://developers.google.com/appengine/docs/go/overview" rel="noreferrer">"multiple requests may be handled concurrently by a given instance"</a>, which can lead to race conditions and unexpected behavior with this plan.  (One request sets it, another sets it, the first one accesses it and gets the wrong <code>appengine.Context</code> object.)</p>
  
  <p>I could in theory store the <code>appengine.Context</code> to datastore, but then I'd have to pass some request-specific identifier down the logic layers to the service-specific modules identifying which <code>appengine.Context</code> object in datastore is the one for the current request, which would again couple things I don't think should be coupled.  (And, it would increase my application's datastore usage.)</p>
  
  <p>I could also pass the <code>appengine.Context</code> object down the whole logic chain with the type <code>interface{}</code> the whole way and have any module which doesn't need the <code>appengine.Context</code> object ignore it.  That would avoid tying most of my application to anything <em>specific</em>.  That also seems highly messy, however.</p>
  
  <p>So, I'm at a bit of a loss how to cleanly ensure the App-Engine-specific modules which need the <code>appengine.Context</code> object can get it.  Hopefully you folks can give me a solution I have yet to think of myself.</p>
  
  <p>Thanks in advance!</p>