Created
June 7, 2011 16:17
-
-
Save XULRunner42/1012586 to your computer and use it in GitHub Desktop.
About Serialization in GO: GOB
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
The serialization demo implements back-and-forth of typed structs into files. It's pretty cool! |
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 goopy | |
import "gob" | |
import "os" | |
type PersonInfo struct { | |
Name string | |
Age int | |
} | |
func (p *PersonInfo)SerializePersonInfoToGob(to_file string) (os.Error) { | |
/* Open file and check for error state */ | |
file_handle, err := os.OpenFile(to_file, os.O_WRONLY|os.O_CREATE, 0600) | |
if err != nil { | |
return err | |
} | |
/* Automatically close when we finish in this function, consider | |
* with open(to_string) as file_handle. */ | |
defer file_handle.Close() | |
/* Serialize data out. */ | |
gob.NewEncoder(file_handle).Encode(p) | |
return nil | |
} | |
func NewPersonInfoFromGob(from_file string) (*PersonInfo, os.Error) { | |
file_handle, err := os.OpenFile(from_file, os.O_RDONLY, 0600) | |
if err != nil { | |
return nil, err | |
} | |
defer file_handle.Close() | |
var person PersonInfo | |
decoder := gob.NewDecoder(file_handle) | |
err = decoder.Decode(&person) | |
return &person, err | |
} | |
func NewPersonInfo(name string, age int) (*PersonInfo){ | |
return &PersonInfo{Name: name, Age: age} | |
} |
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 "log" | |
import "flag" | |
import "goopy" | |
/* File we read and write from. This is required in any case. */ | |
var gobfile *string = flag.String( | |
"gobfile", "data.gob", "A Go Pickle of a Different Flavor.") | |
/* Read mode? We default to this in order to be non-destructive. | |
* we'll pull Gob data out and just dump it to the screen. | |
*/ | |
var writing *bool = flag.Bool( | |
"writing", false, "If specified, we write the command line data.") | |
/* Required only in writing scenario */ | |
var age *int = flag.Int("age", -1, "Age for person record.") | |
var name *string = flag.String("name", "", "Name for person record.") | |
func main() { | |
defer func() { | |
if err := recover(); err != nil { | |
log.Println("Fatal Error Encountered: ", err) | |
} | |
}() | |
flag.Parse() | |
if *writing { | |
person_info := goopy.NewPersonInfo(*name, *age) | |
person_info.SerializePersonInfoToGob(*gobfile) | |
log.Println("Serialization Complete") | |
} else { | |
person_info,err := goopy.NewPersonInfoFromGob(*gobfile) | |
if err != nil { | |
panic(err) | |
} | |
log.Printf("Read Complete (Dump): %+v", person_info) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment