Created
January 14, 2016 10:28
-
-
Save wangkuiyi/eadeb8a5d1c8baaff0e0 to your computer and use it in GitHub Desktop.
This sample program shows how to read Gob-encoded records out from a file.
This file contains 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 ( | |
"encoding/gob" | |
"io" | |
"log" | |
"os" | |
) | |
func main() { | |
type Person struct { | |
Name string | |
} | |
ps := []Person{ | |
Person{Name: "Yi"}, | |
Person{Name: ""}} | |
out, e := os.Create("/tmp/learn-gob") | |
if e != nil { | |
log.Panic(e) | |
} | |
en := gob.NewEncoder(out) | |
for _, p := range ps { | |
if e := en.Encode(&p); e != nil { | |
log.Panic(e) | |
} | |
} | |
out.Close() | |
in, e := os.Open("/tmp/learn-gob") | |
if e != nil { | |
log.Panic(e) | |
} | |
de := gob.NewDecoder(in) | |
for { | |
var p Person | |
if e := de.Decode(&p); e != nil { | |
if e == io.EOF { | |
break | |
} else { | |
log.Panic(e) | |
} | |
} | |
log.Printf("%+v\n", p) | |
} | |
in.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment