From a6edcd222e7f3183985f7c5850df3affc02c4b99 Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Sun, 25 Aug 2019 17:16:12 +0200 Subject: [PATCH] Add marshalling functions for gob This removes a lot of the boilerplate code that comes with (un)marshalling to gob encoding in Go. --- gobmarsh/marshal.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 gobmarsh/marshal.go diff --git a/gobmarsh/marshal.go b/gobmarsh/marshal.go new file mode 100644 index 0000000..baa0154 --- /dev/null +++ b/gobmarsh/marshal.go @@ -0,0 +1,20 @@ +package gobmarsh + +import ( + "bytes" + "encoding/gob" +) + +func Marshal(v interface{}) ([]byte, error) { + b := new(bytes.Buffer) + err := gob.NewEncoder(b).Encode(v) + if err != nil { + return nil, err + } + return b.Bytes(), nil +} + +func Unmarshal(data []byte, v interface{}) error { + b := bytes.NewBuffer(data) + return gob.NewDecoder(b).Decode(v) +}