-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgob.go
More file actions
37 lines (32 loc) · 697 Bytes
/
gob.go
File metadata and controls
37 lines (32 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package rose
import (
"bytes"
"encoding/gob"
)
func GobSerializeErr(data interface{}) ([]byte, error) {
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
err := encoder.Encode(data)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// GobSerialize Gob 序列化
func GobSerialize(data interface{}) []byte {
bt, _ := GobSerializeErr(data)
return bt
}
func GobDeserializeErr(data []byte, obj interface{}) error {
buf := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buf)
err := decoder.Decode(obj)
if err != nil {
return err
}
return nil
}
// GobDeserialize 反序列化
func GobDeserialize(data []byte, obj interface{}) {
_ = GobDeserializeErr(data, obj)
}