mirror of
https://github.com/xxjwxc/public.git
synced 2025-09-26 20:01:19 +08:00
Update serializing.go
This commit is contained in:
@@ -1,41 +1,42 @@
|
||||
package serializing
|
||||
|
||||
import(
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
)
|
||||
|
||||
/*
|
||||
适用类型:二进制到struct相互转换
|
||||
|
||||
使用方法:
|
||||
b, err := serializing.Encode(data)
|
||||
if err != nil {
|
||||
//错误处理
|
||||
}
|
||||
if err := serializing.Decode(b, &to); err != nil {
|
||||
b, err := serializing.Encode(data)
|
||||
if err != nil {
|
||||
//错误处理
|
||||
}
|
||||
if err := serializing.Decode(b, &to); err != nil {
|
||||
//错误处理
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
功能:序列化
|
||||
功能:序列化
|
||||
*/
|
||||
func 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 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 Decode(data []byte,to interface{}) error{
|
||||
buf := bytes.NewBuffer(data)
|
||||
dec := gob.NewDecoder(buf)
|
||||
return dec.Decode(to)
|
||||
}
|
||||
func Decode(data []byte, to interface{}) error {
|
||||
buf := bytes.NewBuffer(data)
|
||||
dec := gob.NewDecoder(buf)
|
||||
return dec.Decode(to)
|
||||
}
|
||||
|
Reference in New Issue
Block a user