Created
May 31, 2015 09:14
-
-
Save duguying/e5f34359f19a7f2abc2d to your computer and use it in GitHub Desktop.
golang object serialize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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