github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xcodec/xgob/gob.go (about) 1 package xgob 2 3 import ( 4 "bytes" 5 "encoding/gob" 6 7 "github.com/go-board/x-go/xcodec" 8 ) 9 10 const Name = "gob" 11 12 type gobc struct{} 13 14 func (gobc) Name() string { return Name } 15 16 func (gobc) Marshal(v interface{}) ([]byte, error) { 17 b := &bytes.Buffer{} 18 err := gob.NewEncoder(b).Encode(v) 19 return b.Bytes(), err 20 } 21 22 func (gobc) Unmarshal(data []byte, v interface{}) error { 23 r := bytes.NewReader(data) 24 return gob.NewDecoder(r).Decode(v) 25 } 26 27 func init() { 28 xcodec.Register(gobc{}) 29 }