Skip to content

Instantly share code, notes, and snippets.

@hoshi-takanori
Created May 27, 2015 08:30
Show Gist options
  • Save hoshi-takanori/5339c64568b7ffb3883a to your computer and use it in GitHub Desktop.
Save hoshi-takanori/5339c64568b7ffb3883a to your computer and use it in GitHub Desktop.
How to use bytes.Buffer with gob encoder/decoder.
package main
import (
"bytes"
"encoding/gob"
"io"
)
type Data struct {
A int
B string
C bool
}
func writeGob(w io.Writer, data []Data) {
enc := gob.NewEncoder(w)
enc.Encode(data)
}
func readGob(r io.Reader) (data []Data) {
dec := gob.NewDecoder(r)
dec.Decode(&data)
return
}
func printData(data []Data) {
for i, d := range data {
println(i, d.A, d.B, d.C)
}
}
func main() {
src := []Data{
Data{123, "abc", true},
Data{456, "def", false},
Data{789, "xyz", true},
}
var buf bytes.Buffer
writeGob(&buf, src)
dst := readGob(&buf)
printData(dst)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment