Go: how to convert struct to [] byte?

I am trying to use "appengine / memcache" to store data in cache, memcache.Item Value - [] byte

how to convert struct to [] bytes to store it?

eg:

type Link struct { Files []string } 
+6
source share
2 answers

See type memcache.Codec, this can be used to convert memcache elements. The appengine / memcache package contains two ready-made codecs, memcache.Gob and memcache.JSON. You use these codecs instead of directly calling to store and retrieve elements from the cache, for example, for example, for an element encoded in gob:

  item := &memcache.Item{ Key: myCacheKey, Object: &myLinkVar, } err := memcache.Gob.Set(context, item) 
+9
source

The encoding/gob is probably the best option.

You can also use the encoding/json package.

If you use encoding/json , you get the ability to read values ​​from languages ​​other than Go.

If you use encoding/gob , you get more speed.

+2
source

All Articles