Retrieving An Array From Memcache In Golang

Here’s a short code example showing how to extract an array from memcache.

CustomStruct is the name of the struct the app uses, and “memcache_key” is the key that the memcache entry is stored under. C represents an appengine.Context reference. The array is extracted to the variable item_list which can then be manipulated and accessed using regular slice semantics.

//Declares the variable to unmarshal the 
//memcache item into. We need to declare 
//it first so the JSON unmarshaller 
//understands what to extract.
var item_list []CustomStruct
_, err = memcache.JSON.Get(c, "memcache_key", &item_list)
if err != nil {
    //Memcache failed to extract the list.
    c.Infof("Unable to retrieve list from Memcache: %v", err)