This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("bytes" "Encoding/gob" "FMT")//--------------------//encode//data encoding gob//func with Encode interface{}) ([]byte, error) {buf: = bytes. Newbuffer (nil) enc: = Gob. Newencoder (BUF) Err: = Enc. Encode (data) if err! = Nil {return nil, Err}return buf. Bytes (), nil}//-------------------///decode//data decoding with gob//func Decode (data []byte, to interface{}) error {buf: = Bytes. Newbuffer (data) Dec: = Gob. Newdecoder (BUF) return Dec. Decode (to)}type User struct {namestringageint}type out struct {ageintnamestring}func New () *user {return &user{name: "Viney", Age:32}}func Main () {//instantiation Useru: = New ()//to user code B, err: = Encode (u) if err! = Nil {fmt. Println ("Encode fail:" + err.) Error ())}//to user decoding var out outif err: = Decode (b, &out); Err! = Nil {fmt. Println ("Decode fail:" + err.) Error ())}fmt. Println (Out)}
For the use of the Go language gob package, please refer to:
Http://golang.org/pkg/encoding/gob