title: Is there a better way to use channels to ensure synchronous access to the map?
body: <p>I'm using Go maps as an in memory cache and channels to ensure synchronous access.</p>
  
  <p>My "session" package defines the cache as: map[string]*SessionData</p>
  
  <p>SessionData is a struct also defined in the package, along with other access functions as seen in the code.</p>
  
  <pre><code>GetWebPage(rw http.ResponseWriter, req *http.Request) {
  var sd *session.SessionData
  var sessTkn string
  
  cookie, err := req.Cookie("sesstoken")
  
  if err == nil { // cookie found
      sessTkn = cookie.Value
      // Check for cache entry for this token,
      // using a channel to protect the map and return
      // a pointer to the cached data if it exists
      sdc := make(chan *session.SessionData, 1)
      go session.GetSessionFromCache(sessTkn, sdc)
      sd = &lt;-sdc
  
      if sd == nil { // sessTkn not in the cache
  
          // This is test data to simplify example 
          sv := make([]string, 4)
          sv[0] = sessTkn
          iv := make([]int, 3)
          iv[0] = 100
          iv[1] = 1000
  
          sdc := make(chan *session.SessionData, 1)
          go session.NewSessionData(sv, iv, false, false, sdc)
          session.SC[sessTkn] = &lt;-sdc
      }
  
  // Is this necessary? Is there a better way?
  // ---------------------------------------      
      if sd == nil {
          sdc = make(chan *session.SessionData, 1)
          go session.GetSessionFromCache(sessTkn, sdc)
          sd = &lt;-sdc      
      }
  // ---------------------------------------      
  
      fmt.Println(sd) // just to prove that it works in both cases
  }   
  // The rest of the handler code follows
  </code></pre>