1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00

Structural binary serialization

This commit is contained in:
2022-04-07 10:29:48 +08:00
parent 8736d84913
commit a872181c66
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package bytesconv
import (
"bytes"
"encoding/gob"
)
func Encode(v any) ([]byte, error) {
var buf = new(bytes.Buffer)
enc := gob.NewEncoder(buf)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func Decode(b []byte, out any) error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
return dec.Decode(out)
}