With the development of Internet technology, more and more novice programmers are interested in golang language. As an emerging programming language, golang is increasingly used by programmers. Proficiency in the golang language can help programmers quickly develop high-quality applications, thereby improving their work efficiency. In this article, we will explore how to build protobuf through golang.
1. Introduction to protobuf
Protobuf, the full name is Protocol Buffers, is a protocol that describes data format, data storage, and data exchange, and provides interface libraries for different programming languages. protobuf can serialize structured data, effectively compress storage space, and has good scalability, making it very suitable for use on various distributed systems.
2. Install protobuf
To use protobuf, we first need to install protobuf on the system. Below, we will introduce how to install protobuf on a Linux system.
Before installing protobuf, you need to install some dependent libraries first. We can use the following command to install dependent libraries:
sudo apt-get update sudo apt-get install autoconf automake libtool curl make g++ unzip
We can go to the official website of protobuf to download the protobuf source code package: https://github. com/protocolbuffers/protobuf/releases. Select the version suitable for your current operating system, and after downloading, extract it to the specified path.
Execute the following command in the decompressed protobuf source code directory:
./autogen.sh ./configure make make check sudo make install sudo ldconfig
If the above steps are executed successfully, then protobuf will be The installation has been successful.
3. Using protobuf
After installing protobuf, we can start using protobuf. The following is a simple example of the steps to use protobuf through golang:
We need to define the data structure in the .gproto file. For example, we define an Employee data structure, which is defined as follows:
syntax = "proto3"; package myproto; message Employee { string name = 1; int32 id = 2; string email = 3; }
The above code defines a data structure named Employee, which includes three fields: name, id, and email. Each field has a unique identification number used for communication between different implementations.
After defining the data structure, we need to use the protobuf compiler to convert it into golang code. Use the following command to generate golang files:
protoc --go_out=. employee.proto
where employee.proto is a defined data structure file, and the --go_out option indicates generating golang code. This command will generate the employee.pb.go file in the same directory.
After generating the golang code, we can use it in golang. Following is a sample program that creates an Employee object, serializes it, and then deserializes it.
package main import ( "bytes" "fmt" "myproto" "strconv" "github.com/golang/protobuf/proto" ) func main() { // 创建一个Employee对象 employee := &myproto.Employee{ Name: "张三", Id: 10001, Email: "zhangsan@qq.com", } fmt.Println("Employee对象:", employee) // 将Employee对象序列化为二进制数据 data, err := proto.Marshal(employee) if err != nil { fmt.Println("序列化失败:", err) return } fmt.Println("序列化后的二进制数据:", data) // 将二进制数据反序列化为Employee对象 newEmployee := &myproto.Employee{} err = proto.Unmarshal(data, newEmployee) if err != nil { fmt.Println("反序列化失败:", err) return } fmt.Println("反序列化后的Employee对象:", newEmployee) // 检查序列化和反序列化是否成功 if newEmployee.Name != employee.Name || newEmployee.Id != employee.Id || newEmployee.Email != employee.Email { fmt.Println("反序列化结果与原始对象不一致!") return } fmt.Println("序列化和反序列化成功!") }
In the above code, we use the proto.Marshal and proto.Unmarshal methods provided by protobuf to serialize and deserialize the Employee object. If the program is executed successfully, then we can see that the output result is:
Employee对象: name:"张三" id:10001 email:"zhangsan@qq.com" 序列化后的二进制数据: [10 6 230 181 139 229 173 151 16 1 26 12 122 104 97 110 103 115 97 110 64 113 113 46 99 111 109] 反序列化后的Employee对象: name:"张三" id:10001 email:"zhangsan@qq.com" 序列化和反序列化成功!
IV. Summary
In this article, we introduced how to build protobuf through golang. First we installed the protobuf dependency library, then compiled protobuf, and finally used protobuf in golang for serialization and deserialization. In addition to being used in golang, protobuf can also be used in other programming languages such as Python and Java, which is very suitable for data exchange in distributed systems. I hope this article can help programmers better learn and master protobuf technology.
The above is the detailed content of How to build protobuf through golang. For more information, please follow other related articles on the PHP Chinese website!