Home >Backend Development >Golang >Using Google Protocol Buffers for data serialization in Go language

Using Google Protocol Buffers for data serialization in Go language

WBOY
WBOYOriginal
2023-06-16 14:13:512004browse

Google Protocol Buffers (hereinafter referred to as ProtoBuf) is a lightweight and efficient data serialization format that is widely used for data transmission and storage in distributed systems. As a modern programming language, Go language is also very friendly in supporting ProtoBuf. This article will introduce how to use ProtoBuf for data serialization in Go language.

1. Install ProtoBuf

Before you start using ProtoBuf, you need to install it first. You can download the binary package corresponding to the operating system from the official website (https://developers.google.com/protocol-buffers/) for installation, or you can use the system package manager to install (such as Ubuntu: sudo apt-get install protobuf-compiler ).

After the installation is complete, you can use the following command to check whether the installation is successful:

$ protoc --version
libprotoc 3.6.1

2. Define the message format

Before using ProtoBuf for data serialization, the message format needs to be defined first. In ProtoBuf, the message format is defined through .proto files. For example, we define a message format named Person:

syntax = "proto3";

message Person {
string name = 1;
int32 age = 2;
repeated string address = 3;
}

where syntax specifies the syntax version used by ProtoBuf, message defines a message type, and name, age, and address are fields in the message. Fields are specified by numeric identifiers, which must be unique.

It is worth noting that ProtoBuf supports nested message types, and one message type can be defined as a field of another message type.

3. Compile and generate Go code

ProtoBuf needs to compile the .proto file into the code of the corresponding language for use in the program. In the Go language, you can use the protoc-gen-go plug-in for compilation, or you can use the github.com/golang/protobuf/protoc-gen-go plug-in for compilation. This article takes the first method as an example to introduce.

First you need to install the plug-in:

$ go get -u github.com/golang/protobuf/protoc-gen-go

Then use the following command to compile the .proto file:

$ protoc --go_out=. *.proto

This will generate a Go file named person.pb.go, which contains the definition of the Person message type and serialization and deserialization ation related methods.

4. Use ProtoBuf for data serialization

It is very simple to use ProtoBuf for data serialization in the program. Taking the Person message as an example, we can use the following code to serialize a Person object into binary data:

package main

#import (

"log"

"github.com/golang/protobuf/proto"

)

func main() {

p := &Person{
    Name:    "Tom",
    Age:     20,
    Address: []string{"Shanghai", "Beijing"},
}

data, err := proto.Marshal(p)
if err != nil {
    log.Fatal("marshaling error: ", err)
}

log.Println(data)

}

In the above code, we first create a Person object p, then call the proto.Marshal method to serialize it into binary data and print it come out. Note that when using the proto.Marshal method, you need to pass in a pointer to the Person object.

5. Use ProtoBuf for data deserialization

Similar to data serialization, using ProtoBuf for data deserialization is also very simple. We can use the following code to deserialize the serialized binary data into a Person object:

package main

import (

"log"

"github.com/golang/protobuf/proto"

)

func main() {

data := []byte{10, 3, 84, 111, 109, 16, 20, 26, 8, 83, 104, 97, 110, 103, 104, 97, 105, 18, 7, 66, 101, 105, 106, 105, 110, 103}

p := &Person{}
err := proto.Unmarshal(data, p)

if err != nil {
    log.Fatal("unmarshaling error: ", err)
}

log.Println(p)

}

In the above code, we first define a binary data, then call the proto.Unmarshal method to deserialize it into a Person object and print it out .

6. Summary

In this article, we introduced how to use ProtoBuf for data serialization in Go language. It should be noted that when using ProtoBuf for data serialization and deserialization, the message format must be defined and compiled to generate code in the corresponding language. In addition, using ProtoBuf for data serialization and deserialization is very simple. You only need to call the corresponding method to complete.

The above is the detailed content of Using Google Protocol Buffers for data serialization in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn