Created
May 27, 2015 08:30
-
-
Save hoshi-takanori/5339c64568b7ffb3883a to your computer and use it in GitHub Desktop.
How to use bytes.Buffer with gob encoder/decoder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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