gob is an encoding/decoding tool for data structure serialization that comes with the Golang package. Encoder is used for encoding and Decoder is used for decoding. Gob uses the io.Writer interface to create an Encoder object through the NewEncoder() function and calls the Encode() method to implement the encoding operation; uses the io.Reader interface to create a Decoder object through the NewDecoder() function and calls the Decode() method to complete the decoding operation.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
The full name of gob is: Go binary
Golang comes with a data structure serialization encoding/decoding tool, which means gob can speak A data structure in go is serialized into something, and it can also be deserialized! We will look at what serialization becomes later. Whether it turns into a string or a binary stream, it doesn't matter what it turns into. Anyway, the function is serialization.
When using Gob, we need to pay attention to the Encoder and Decoder objects. As the names suggest, one is used when encoding and the other is used when decoding. Let’s take a look at how to obtain these two objects first:
So it is clear that these two functions need to be called to obtain the Encoder and Decoder objects. Note that the parameters here are io.Writer and io.Reader interface types. We introduced these two interfaces in the previous lecture, so the parameters required here are objects that implement the io.Writer and io.Reader interface types respectively.
Encoder and Decoder each have one main method:
Seeing here we can already get the following conclusion :
Gob uses the io.Writer interface, creates the Encoder object through the NewEncoder() function, and implements the encoding operation by calling the Encode() method; uses the io.Reader interface, creates the Decoder object through the NewDecoder() function, and Call the Decode() method to complete the decoding operation!
Next, let’s try to use this Encoder and Decoder to get started with gob easily. Let’s look at the first example
Example 1: Data structure and bytes.Buffer Conversion between (encoded into byte slices)
1package main 2 3import ( 4 "bytes" 5 "fmt" 6 "encoding/gob" 7 "io" 8) 9 10//准备编码的数据 11type P struct { 12 X, Y, Z int 13 Name string 14} 15 16//接收解码结果的结构 17type Q struct { 18 X, Y *int32 19 Name string 20} 21 22func main() { 23 //初始化一个数据 24 data := P{3, 4, 5, "CloudGeek"} 25 //编码后得到buf字节切片 26 buf := encode(data) 27 //用于接收解码数据 28 var q *Q 29 //解码操作 30 q = decode(buf) 31 //"CloudGeek": {3,4} 32 fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y) 33 34} 35 36func encode(data interface{}) *bytes.Buffer { 37 //Buffer类型实现了io.Writer接口 38 var buf bytes.Buffer 39 //得到编码器 40 enc := gob.NewEncoder(&buf) 41 //调用编码器的Encode方法来编码数据data 42 enc.Encode(data) 43 //编码后的结果放在buf中 44 return &buf 45} 46 47func decode(data interface{}) *Q { 48 d := data.(io.Reader) 49 //获取一个解码器,参数需要实现io.Reader接口 50 dec := gob.NewDecoder(d) 51 var q Q 52 //调用解码器的Decode方法将数据解码,用Q类型的q来接收 53 dec.Decode(&q) 54 return &q 55}
Example 2: Serialization and deserialization of data structure to file
1package main 2 3import ( 4 "encoding/gob" 5 "os" 6 "fmt" 7) 8 9//试验用的数据类型 10type Address struct { 11 City string 12 Country string 13} 14 15//序列化后数据存放的路径 16var filePath string 17 18func main() { 19 filePath = "./address.gob" 20 encode() 21 pa := decode() 22 fmt.Println(*pa) //{Chengdu China} 23} 24 25//将数据序列号后写到文件中 26func encode() { 27 pa := &Address{"Chengdu", "China"} 28 //打开文件,不存在的时候新建 29 file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0666) 30 defer file.Close() 31 32 //encode后写到这个文件中 33 enc := gob.NewEncoder(file) 34 enc.Encode(pa) 35} 36 37//从文件中读取数据并反序列化 38func decode() *Address { 39 file, _ := os.Open(filePath) 40 defer file.Close() 41 42 var pa Address 43 //decode操作 44 dec := gob.NewDecoder(file) 45 dec.Decode(&pa) 46 return &pa 47}
None of the above two examples Difficult, I removed the error handling and other codes, and tried my best to annotate every piece of code. After reading these two examples patiently, you should be able to understand the essence of gob's encode and decode.
Based on understanding what gob is, if you need to use gob for development, it is recommended to read the official documentation in detail to learn more details: https://golang.org/pkg/encoding/gob/
【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of What is golang Gob. For more information, please follow other related articles on the PHP Chinese website!