Skip to main content

Command Palette

Search for a command to run...

Serializing Objects To Binary Format

Updated
2 min readView as Markdown
Serializing Objects To Binary Format
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources
  • 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.

Create the gob.go file 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}`,
        u.FirstName, u.LastName)
}

func main() {

    var buff bytes.Buffer

    // Encode value
    enc := gob.NewEncoder(&buff)
    user := User{
        "sangam",
        "biradar",
        24,
        true,
    }
    enc.Encode(user)
    fmt.Printf("%X\n", buff.Bytes())

    // Decode value
    out := User{}
    dec := gob.NewDecoder(&buff)
    dec.Decode(&out)
    fmt.Println(out.String())

    enc.Encode(user)
    out2 := SimpleUser{}
    dec.Decode(&out2)
    fmt.Println(out2.String())

}

output:

sangam:golang-daily sangam$ go run gob.go
40FF81030101045573657201FF82000104010946697273744E616D65010C0001084C6173744E616D65010C0001034167650104000106416374697665010200000018FF82010673616E67616D0107626972616461720130010100
{"FirstName":sangam,"LastName":biradar,
                                         "Age":24,"Active":true }
{"FirstName":sangam,"LastName":biradar}
sangam:golang-daily sangam$

How it works...

  • The gob serialization and deserialization need the Encoder and Decoder. The gob.NewEncoder function creates the Encoder with the underlying Writer.
  • Each call of the Encode method will serialize the object into a gob format. The gob format itself is the self-describing binary format. This means each serialized struct is preceded by its description.

  • To decode the data from the serialized form, the Decoder must be created by calling the gob.NewDecoder with the underlying Reader. The Decode then accepts the pointer to the structure where the data should be deserialized.

3 views

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !