Skip to content

Instantly share code, notes, and snippets.

@duguying
Created May 31, 2015 09:14
Show Gist options
  • Save duguying/e5f34359f19a7f2abc2d to your computer and use it in GitHub Desktop.
Save duguying/e5f34359f19a7f2abc2d to your computer and use it in GitHub Desktop.
golang object serialize
import (
"bytes"
"encoding/gob"
)
func (this *Cache) encode(data interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
err := enc.Encode(data)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (this *Cache) decode(data []byte, to interface{}) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
return dec.Decode(to)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment