SoFunction
Updated on 2025-03-03

Use of Go built-in serialization library gob

Overview

Gob is a format for Go language to serialize and deserialize program data in binary form, and can be found in the encoding package. Data in this format is simply called Gob (the abbreviation of Go binary). Similar to Python's "pickle" and Java's "Serialization".

Gob is the same as JSON pack and other methods, and the sender uses Encoder to encode the data structure. After receiving the message on the receiving side, the receiving side uses Decoder to change the serialized data to cost the local variable.

A typical application of Gob is in the standard library net/rpc.

The gob library is part of the Go standard library, which is used to serialize Go's data types into byte streams, or deserialize the byte streams into Go's data types. The data types supported by the gob​ library include basic data types, structures, arrays, slices, maps, channels, etc.

The use of the gob library is very simple. You only need to call the Encode() function to serialize the data type into a byte stream, or call the Decode() function to deserialize the byte stream into a data type. In addition, the gob​ library also supports registered data types and custom codecs.

Example of using gob library

The following is a few examples to demonstrate how to use the gob​ library.

1. Serialize and deserialize basic data types

package main
import (
    "bytes"
    "encoding/gob"
    "fmt"
)
func main() {
    var buf 
    // Serialization    encoder := (&buf)
    err := (123)
    if err != nil {
        panic(err)
    }
    // Deserialization    decoder := (&buf)
    var i int
    err = (&i)
    if err != nil {
        panic(err)
    }
    (i)
}

In the example above, we first create a variable buf of type, and then use the NewEncoder() function of the gob library to create an encoder encoder, and set the output stream of the encoder to buf. Next, we serialize the integer 123​ into a byte stream and save the serialization result in buf​. Finally, we use the NewDecoder() function of the gob library to create a decoder decoder, and set the input stream of the decoder to buf. Then, we deserialize the serialized data into integers using decoder and save the result in variable i​. Finally, we print out the value of variable i, and the output is 123.

2. Serialize and deserialize structures

package main
import (
    "bytes"
    "encoding/gob"
    "fmt"
)
type Person struct {
    Name string
    Age  int
}
func main() {
    var buf 
    // Serialization    encoder := (&buf)
    p := Person{Name: "Alice", Age: 20}
    err := (p)
    if err != nil {
        panic(err)
    }
    // Deserialization    decoder := (&buf)
    var p2 Person
    err = (&p2)
    if err != nil {
        panic(err)
    }
    (p2)
}

In the example above, we define a structure Person​, which has two fields Name​ and Age​. We first create a variable buf of type, and then use the NewEncoder() function of the gob library to create an encoder encoder, and set the output stream of the encoder to buf. Next, we create a variable p​ of type Person, serialize it into a byte stream, and save the serialization result in buf​. Finally, we use the NewDecoder() function of the gob library to create a decoder decoder, and set the input stream of the decoder to buf. Then, we deserialize the serialized data to Person type using decoder and save the result in variable p2. Finally, we print out the value of variable p2​ and the output is {Alice 20}​.

3. Register data type

In the example above, when we serialize and deserialize the Person struct, we need to use the NewEncoder() and NewDecoder() functions to create the encoder and decoder. This would be very troublesome if we need to serialize and deserialize the same type multiple times. To solve this problem, the gob​​ library provides the Register() function, which can register a data type into the gob​​ library, so that the encoder and decoders can be used directly during serialization and deserialization.

Here is an example:

package main
import (
    "bytes"
    "encoding/gob"
    "fmt"
)
type Person struct {
    Name string
    Age  int
}
func main() {
    var buf 
    // Register data type    (Person{})
    // Serialization    encoder := (&buf)
    p := Person{Name: "Alice", Age: 20}
    err := (p)
    if err != nil {
        panic(err)
    }
    // Deserialization    decoder := (&buf)
    var p2 Person
    err = (&p2)
    if err != nil {
        panic(err)
    }
    (p2)
}

In the example above, we use the Register() function of the gob library to register the Person structure into the gob library. Then, we can use the encoder and decoder directly when serializing and deserializing.

This is the end of this article about the use of Go built-in serialization library gob. For more related Go gob content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!