1 Star 2 Fork 0

api-go/iris

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
gob.go 1.61 KB
Copy Edit Raw Blame History
package memstore
import (
"bytes"
"encoding/gob"
"io"
"time"
)
// why?
// on the future we may change how these encoders/decoders
// and we may need different method for store and other for entry.
func init() {
gob.Register(Store{})
gob.Register(Entry{})
gob.Register(time.Time{})
}
// GobEncode accepts a store and writes
// as series of bytes to the "w" writer.
func GobEncode(store Store, w io.Writer) error {
enc := gob.NewEncoder(w)
err := enc.Encode(store)
return err
}
// GobSerialize same as GobEncode but it returns
// the bytes using a temp buffer.
func GobSerialize(store Store) ([]byte, error) {
w := new(bytes.Buffer)
err := GobEncode(store, w)
return w.Bytes(), err
}
// GobEncodeEntry accepts an entry and writes
// as series of bytes to the "w" writer.
func GobEncodeEntry(entry Entry, w io.Writer) error {
enc := gob.NewEncoder(w)
err := enc.Encode(entry)
return err
}
// GobSerializeEntry same as GobEncodeEntry but it returns
// the bytes using a temp buffer.
func GobSerializeEntry(entry Entry) ([]byte, error) {
w := new(bytes.Buffer)
err := GobEncodeEntry(entry, w)
return w.Bytes(), err
}
// GobDecode accepts a series of bytes and returns
// the store.
func GobDecode(b []byte) (store Store, err error) {
dec := gob.NewDecoder(bytes.NewBuffer(b))
// no reference because of:
// gob: decoding into local type *memstore.Store, received remote type Entry
err = dec.Decode(&store)
return
}
// GobDecodeEntry accepts a series of bytes and returns
// the entry.
func GobDecodeEntry(b []byte) (entry Entry, err error) {
dec := gob.NewDecoder(bytes.NewBuffer(b))
err = dec.Decode(&entry)
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/netscript/iris.git
git@gitee.com:netscript/iris.git
netscript
iris
iris
v8.5.2

Search