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