Go Golang GOB encoding

Source: Internet
Author: User

Golang GOB encoding

2012-08-24 09:47 by Xuan Blade, 5119 Read, 1 reviews, favorites, compilation

Gob is a serialized encoding/decoding tool with a data structure that comes with the Golang package. Encoding uses encoder, decoding uses decoder. A typical application scenario is RPC (remote procedure calls).

Like GOB and JSON, the data structure is encoded by the sending side using encoder. After receiving the message, the receiving end uses decoder to change the cost of the serialized data variable.

One thing to note,

The structure of the sender and the structure of the receiving party do not need to be fully consistent

The default fields in the struct will not be sent. And on the receiving side, it is not necessary to have all the fields corresponding to the corresponding structural attributes. This example in Godoc is very image:

When the sender passes the value of the struct{a, B INT} structure, the receiver can allow the first 9 structures, but the latter 4 structures do not allow.

Personally, this setting is logical: The receiving end only accepts and sends the data structure " similar " to the data. Impersonation is allowed to be similar, but contradictions are not allowed.

Encoding and decoding rules for each type

Integer: Divided into sign int and usign int, which is also seen from the above example, int and uint cannot be decoded to each other. float and int are also not decoded to each other.

Struct,array,slice can be encoded. But function and channel cannot be encoded.

The bool type is encoded as a uint, and 0 is false,2 is true.

The value of a floating-point type is encoded as a value of type float64.

string and []byte Pass is the form of a uint (byte number) + byte[] Encoded

The slice and array are encoded in the form of UINT (array number) + each array encoding.

Maps is encoded in the form of a uint (map number) + key value

A Struct is encoded according to a pair of pairs (attribute name + property value). Where the property value is its own corresponding GOB encoding. As I said earlier, if you have a property value of 0 or NULL, this property is ignored directly. The ordinal of each property is determined by the encoding time order, incrementing from 0. The struct will start with 1 for serialization before serialization, and 0 for the end of serialization. That is, the serialization of a struct is encoded according to the "-1 (0 attribute 1 name attribute 1 value) (1 attribute 2 name attribute 2 value) 0".

A very important point:

The attribute in a struct should be public, that is, it should start with an uppercase letter.

This can be accessed by a function outside the package!! (Thank you treapdb reminder)

Functions provided by Gob

Encode and Decode

For encoder and decoder you can look at this example:

1234567891011121314151617181920212223242526272829303132333435363738 package mainimport (    "bytes"    "encoding/gob"    "fmt"    "log")type P struct {    X, Y, Z int    Name    string}type Q struct {    X, Y *int32    Name string}func main() {    var network bytes.Buffer            enc := gob.NewEncoder(&network)     dec := gob.NewDecoder(&network)     // Encode (send) the value.    err := enc.Encode(P{3, 4, 5, "Pythagoras"})    if err != nil {        log.Fatal("encode error:", err)    }    // Decode (receive) the value.    var q Q    err = dec.Decode(&q)    if err != nil {        log.Fatal("decode error:", err)    }    fmt.Println(q)    fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y) }

All encoder and decoder constructors have an IO structure and need to develop which IO you will use to encode and decode the transmission.

This code should pay attention to a few places: 1 P and Q is two structures, should be said to be "similar" two struct 2 encode is to pass the structure, but the decode function parameter is a pointer!

This is said in Godoc:

F E is nil, the value would be discarded. Otherwise, the value underlying e must is a pointer to the correct type for the next data item received.

If the decode parameter is not nil, then it must be a pointer.

3 If you pass encode into a pointer, that is
1234567891011121314151617181920 func main() {    var network bytes.Buffer        // Stand-in for a network connection    enc := gob.NewEncoder(&network) // Will write to network.    dec := gob.NewDecoder(&network) // Will read from network.    // Encode (send) the value.    err := enc.Encode(&P{3, 4, 5, "Pythagoras"})    if err != nil {        log.Fatal("encode error:", err)    }    // Decode (receive) the value.    var q Q    err = dec.Decode(&q)    if err != nil {        log.Fatal("decode error:", err)    }    fmt.Println(q)    fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y)}

This function is also not a problem.

Register and Registername

These two methods are required to register the possible types of interface{} when there is a field in the codec that is interface{}. Just look at the following example:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 package mainimport (    "bytes"    "encoding/gob"    "fmt"    "log") type P struct {    X, Y, Z int    Name    interface{}}type Q struct {    X, Y *int32    Name interface{}}type Inner struct {    Test int}func main() {    var network bytes.Buffer        // Stand-in for a network connection    enc := gob.NewEncoder(&network) // Will write to network.    dec := gob.NewDecoder(&network) // Will read from network.        gob.Register(Inner{})        // Encode (send) the value.    inner := Inner{1}    err := enc.Encode(P{1,2,3, inner})    if err != nil {        log.Fatal("encode error:", err)    }    // Decode (receive) the value.    var q Q    err = dec.Decode(&q)    if err != nil {        log.Fatal("decode error:", err)    }    fmt.Println(q)    fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y)}

The gob is used here. Register (inner{}) tells the system: all interface are possible for the Inner structure.

In this example, if you comment on the gob. Register, the system will error.

The Registername is the same as the register, except that the register also has an alias for the type.

Gebencoder and Gobdecoder

This is two interfaces if your data structure implements both interfaces when calling encoder. The corresponding functions of these two structures are called when encode and Decoder.decode

Take a look at the following example:

123456789101112131415161718192021222324252627282930313233343536373839404142 package mainimport (    "bytes"    "encoding/gob"    "fmt"    "log")type P struct {    X, Y, Z int    Name    string}func (this *P)GobEncode() ([]byte, error) {    return []byte{},nil }type Q struct {    X, Y *int32    Name string}func main() {    var network bytes.Buffer            enc := gob.NewEncoder(&network)     dec := gob.NewDecoder(&network)     // Encode (send) the value.    err := enc.Encode(P{3, 4, 5, "Pythagoras"})    if err != nil {        log.Fatal("encode error:", err)    }    // Decode (receive) the value.    var q Q    err = dec.Decode(&q)    if err != nil {        log.Fatal("decode error:", err)    }    fmt.Println(q)    fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y)}

Here my P implements the Gobencoder interface, so in Enc. Func (this *p) Gobencode ([]byte, error) is called when encode

Of course, this function directly returns an empty byte, so it will error when decoding: Decode Error:gob:type mismatch in decoder:want struct type main. Q; Got Non-struct

These two interfaces are exposed to make codec rules for the structure you define for yourself. Of course, if you use your own codec rules, the encoding and decoding process needs to be a pair of

Go Golang GOB encoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.