Home > Article > Backend Development > Encoding and decoding techniques for structure types in Golang functions
Golang is a very powerful programming language that provides many built-in types and functions to help programmers write efficient code quickly. Among them, the structure type is a very commonly used one. When dealing with network communication, data storage and transmission, etc., we usually need to encode structure type data into binary data or serialize it into a string in JSON format for transmission. This article will introduce some coding and decoding techniques for the structure type of Golang functions.
In Golang, the definition of structure type requires the use of the type keyword and the struct keyword. For example, the following defines a Person structure type, which contains two fields: Name and Age:
type Person struct { Name string Age int }
JSON is a lightweight , easy to read and write data exchange format. In Golang, we can use the Marshal() and Unmarshal() functions in the json package to implement JSON encoding and decoding of structure types.
When encoding, we need to convert the structure type into a byte array of []byte type, which can be achieved through the json.Marshal() function. For example, the following example encodes an instance p of type Person into a string in JSON format:
p := Person{"Tina", 28} jsonData, _ := json.Marshal(p) fmt.Println(string(jsonData)) // {"Name":"Tina","Age":28}
When decoding, we need to parse the JSON string into Structure type. This can be achieved through the json.Unmarshal() function. For example, the following example decodes the JSON string jsonStr into a Person type instance:
jsonStr := `{"Name":"Tina", "Age": 28}` var p Person _ = json.Unmarshal([]byte(jsonStr), &p) fmt.Println(p) // {Tina 28}
In the process of network communication or data storage, we It is also necessary to serialize the structure type into a byte sequence for transmission. For example, to serialize the Person type instance p into the byte sequence byteData:
buf := new(bytes.Buffer) binary.Write(buf, binary.LittleEndian, &p) byteData := buf.Bytes() fmt.Printf("%x ", byteData) // 54696e61001c0000
We use the bytes.Buffer and binary.Write() functions to serialize the structure type p into the byte sequence byteData. The second parameter of the binary.Write() function specifies the byte order processing method, here the LittleEndian format is used.
Similarly, we can also use the binary.Read() function to deserialize byteData in the same form, for example:
var q Person err := binary.Read(bytes.NewReader(byteData), binary.LittleEndian, &q) if err != nil { fmt.Println(err) } fmt.Println(q) // {Tina 28}
Gob is an encoding format that comes with Golang and is often used during RPC calls and data transmission. In Golang, the gob package is also provided for Gob encoding and decoding.
We can use the gob.NewEncoder() function to create an encoder instance, and then use its Encode() method to encode the structure type into bytes sequence. For example, encoding an instance p of the Person type into a byte sequence byteData:
var byteData bytes.Buffer enc := gob.NewEncoder(&byteData) err := enc.Encode(&p) if err != nil { fmt.Println(err) } fmt.Printf("%x ", byteData.Bytes()) // 026864073544696e61001c
We use the bytes.Buffer and gob.NewEncoder() functions to encode the structure type p into a byte sequence byteData.
When decoding, we also need to use the gob package. Create a decoder instance and use its Decode() method to decode a sequence of bytes. For example, decode the encoded byteData in the above example into an instance q of Person type:
dec := gob.NewDecoder(&byteData) var q Person err := dec.Decode(&q) if err != nil { fmt.Println(err) } fmt.Println(q) // {Tina 28}
Summary
The structure type is a commonly used data type in Golang. It is used in processing network communication and When storing and transmitting data, we often need to encode the structure type into binary data or serialize it into a string for transmission. This article introduces some encoding and decoding techniques for structure types of Golang functions, including JSON encoding and decoding, byte sequence encoding and decoding, and Gob encoding and decoding. These techniques can help us complete data transfer and storage tasks quickly and efficiently.
The above is the detailed content of Encoding and decoding techniques for structure types in Golang functions. For more information, please follow other related articles on the PHP Chinese website!