Serializing objects to binary format
Besides the well-known JSON and XML, Go also offers the binary format, gob. This recipe goes through the basic concept of how to use the gob package.
How to do it...
- Open the console and create the folder
chapter05/recipe10. - Navigate to the directory.
- Create the
gob.gofile with the following content:
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
type User struct {
FirstName string
LastName string
Age int
Active bool
}
func (u User) String() string {
return fmt.Sprintf(`{"FirstName":%s,"LastName":%s,
"Age":%d,"Active":%v }`,
u.FirstName, u.LastName, u.Age, u.Active)
}
type SimpleUser struct {
FirstName string
LastName string
}
func (u SimpleUser) String() string {
return fmt.Sprintf(`{"FirstName":%s,"LastName":%s}`,
...