github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/codec/gob.go (about) 1 package codec 2 3 import ( 4 "bytes" 5 "encoding/gob" 6 "log" 7 ) 8 9 type gobCodec struct { 10 buf bytes.Buffer 11 } 12 13 var Gob SerializeType = RegisterCodec("Gob", newGobCodec()) 14 15 func newGobCodec() *byteCodec { 16 return &byteCodec{} 17 } 18 19 // Encode returns raw slice of bytes. 20 func (c gobCodec) Encode(i interface{}) ([]byte, error) { 21 var network bytes.Buffer // Stand-in for a network connection 22 enc := gob.NewEncoder(&network) // Will write to network. 23 err := enc.Encode(i) 24 if err != nil { 25 log.Fatal("encode error:", err) 26 } 27 28 return network.Bytes(), nil 29 } 30 31 // Decode returns raw slice of bytes. 32 func (c gobCodec) Decode(data []byte, i interface{}) error { 33 var network bytes.Buffer // Stand-in for a network connection 34 35 dec := gob.NewDecoder(&network) // Will read from network. 36 err := dec.Decode(&data) 37 if err != nil { 38 return err 39 } 40 return nil 41 } 42 43 func (c gobCodec) String() string { 44 return "Gob" 45 }