mindoc/utils/gob.go

31 lines
434 B
Go
Raw Normal View History

2017-04-21 18:20:35 +08:00
package utils
import (
"bytes"
"encoding/gob"
)
//解码
func Decode(value string, r interface{}) error {
2017-04-21 18:20:35 +08:00
network := bytes.NewBuffer([]byte(value))
2017-04-21 18:20:35 +08:00
dec := gob.NewDecoder(network)
return dec.Decode(r)
2017-04-21 18:20:35 +08:00
}
//编码
func Encode(value interface{}) (string, error) {
network := bytes.NewBuffer(nil)
2017-04-21 18:20:35 +08:00
enc := gob.NewEncoder(network)
err := enc.Encode(value)
2017-04-21 18:20:35 +08:00
if err != nil {
return "", err
2017-04-21 18:20:35 +08:00
}
return network.String(), nil
}