Writing in the front

In the previous article, Reading and writing Data in Golang (Part 2), we learned how to parse JSON data in Golang. Next, we will learn about Gob encoding.

What is a Gob

Gob is Go’s own format for serializing and deserializing program data in binary form. This data format is simply called Gob (Go Binary).

It is similar to Serialization in the Java language. You can find it in the Encoding package.

What can Gob do

Gob is typically used for the transfer of remote method call (RPC) parameters and results, as well as data transfer between applications and machines.

So, how is it different from JSON, which we’ve used in general?

Because Gob is Go’s own format for serializing and deserializing program data in binary form, it can only be used in a pure Go environment and is not suitable for heterogeneous environments. For example, it can be used to communicate between two Go programs.

The characteristics of the Gob

  1. A Gob file or stream is completely self-describing: all types contained within it have a corresponding description, and can always be decoded with Go without needing to know the contents of the file.

  2. Only exportable fields are encoded, and zero values are ignored.

  3. When decoding structures, only fields that match both the name and compatible type will be decoded.

  4. When a new field is added to the source data type, the Gob decoder client can still work in this way: the decoder client will continue to recognize the previously existing field.

Use Gob to transfer data

Like JSON, Gob uses the generic IO.Writer interface, using the NewEncoder() function to create Encoder objects and call Encode(); The opposite procedure uses the generic IO.Reader interface, creates a Decoder object through the NewDecoder() function and calls Decode().

Look at the following example:

package main

import (
	"bytes"
	"encoding/gob"
	"fmt"
)

/ / cultivate immortality
type Immortal struct {
	Name   string
	Age    int
	Gender string
}

type SimpleImmortal struct {
	Name   string
	Age    int
}

var buf  bytes.Buffer

func main(a) {
	
	var hanli = Immortal{
		Name:   Mr.han "",
		Age:    18,
		Gender: "Men",
	}

	fmt.Println("Send data:",hanli)
	sendMsg(&hanli)
	fmt.Println("Data in BUF:",buf)
	var i SimpleImmortal
	msg, _ := receiveMsg(i)

	fmt.Println("Received data:",msg)
}

func sendMsg(immortal *Immortal) error {
	enc :=gob.NewEncoder(&buf)
	return enc.Encode(immortal)
}

func receiveMsg(immortal SimpleImmortal) (SimpleImmortal,error) {
	dec := gob.NewDecoder(&buf)

	return immortal,dec.Decode(&immortal)

}
Copy the code

Output:

Send data: {Han Li18Male} data in BUF: {[50 255 129 3 1 1 8 73 109 109 111 114 116 97 108 1 255 130 0 1 3 1 4 78 97 109 101 1 12 0 1 3 65 103 101 1 4 0 1 6 71 101 110 100 101 114 1 12 0 0 0 21 255 130 1 6 233 159 169 231 171 139 1 36 1 6 231 148 183 230 128 167 0] 0 0} Received data: {Han Li18}
Copy the code

Write in the last

So that’s the end of Gob. The examples covered in this article can be downloaded here. If my study notes can help you, please give me a thumbs up and encouragement. If there are mistakes and omissions in the article, please help to correct them.

In the next article, we’ll take a look at error handling in Golang.