SoFunction
Updated on 2025-03-05

Go language uses Gob to transfer data

In order for a data structure to be transmitted on the network or saved to a file, it must be encoded and then decoded. Of course, there are already many available encoding methods, such as JSON, XML, Google's protocol buffers, etc. Now there is another way, provided by the Go language encoding/gob package.

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.

The Go language can serialize struct objects through JSON or Gob. Although JSON serialization is more general, using Gob encoding can realize the serialization of struct methods that JSON cannot support. It is also very simple to use Gob package to serialize structs to save locally.

Gob is not an externally definable, language-independent encoding method. Its preferred binary formats rather than text formats like JSON or XML. Gob is not a language different from Go, but uses Go reflection in the encoding and decoding process.

Gob is commonly used for the transfer of remote method call parameters and results, as well as data transfer between applications and machines. How is it different from JSON or XML? Gob is used in pure Go environments, such as communication between two services written in Go. In this way, the service can be implemented more efficiently and optimized.

A Gob file or stream is completely self-described. All types contained in it have a corresponding description and can be decoded in Go without understanding the content of the file.

Only exportable fields will be encoded and zero values ​​will be ignored. When decoding a structure, only fields that match the name and compatible types will be decoded. The Gob decoding client can still work properly in this way when a new field is added to the source data type. The decoding client continues to identify previously existing fields and also provides a lot of flexibility, such as in the sender's view, integers are encoded to variable lengths without fixed lengths, ignoring the specific Go type.

If there is a structure like T:

type T struct { X, Y, Z int }
var t = T{X: 7, Y: 0, Z: 8}

When receiving, you can use a variable u of type U of structure to receive this value:

type U struct { X, Y *int8 }
var u U

When receiving, the value of X is 7 and the value of Y is 0 (the value of Y is not passed from t because it is a zero value) is the same as JSON. Gob uses a common interface to create an Encoder object through the NewEncoder() function and call Encode(). The opposite process uses a common interface to create a Decoder object through the NewDecoder() function and call Decode.

Create gob file

The following is a simple example program to demonstrate how Go creates gob files, the code is as follows:

package main
import (
    "encoding/gob"
    "fmt"
    "os"
)
func main() {
    info := map[string]string{
        "name":    "I",
        "website": "/golang/",
    }
    name := ""
    File, _ := (name, os.O_RDWR|os.O_CREATE, 0777)
    defer ()
    enc := (File)
    if err := (info); err != nil {
        (err)
    }
}

Running the above code will generate a file in the current directory, and the content of the file is as follows:

0eff 8104 0102 ff82 0001 0c01 0c00 0041
ff82 0002 046e 616d 6510 43e8 afad e8a8
80e4 b8ad e696 87e7 bd91 0777 6562 7369
7465 1e68 7474 703a 2f2f 632e 6269 616e
... ...

Read gob file

Reading gob files is as simple as creating gob files. The sample code is as follows:

package main
import (
    "encoding/gob"
    "fmt"
    "os"
)
func main() {
    var M map[string]string
    File, _ := ("")
    D := (File)
    (&M)
    (M)
}

The operation results are as follows:

go run
map[name:my website:/golang/]

This is the end of this article about Go using Go to transmit data. For more related Go languages, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!