Golang Gin 实战(十二)| ProtoBuf
ProtoBuf最近几年也算比较流行,它是一种语言无关,平台无关,并且可以扩展,并结构数据序列化的方法。相比JSON/XML这类文本格式的,ProtoBuf是二进制的,更快更高效。对于这么好的特性,Gin当然是支持的。
通过这篇文章你可以学到:
-
ProtoBuf数据结构
-
ProtoBuf对象如何生成Golang代码
-
Gin服务端如何序列化ProtoBuf输出
-
Go客户端如何反序列化ProtoBuf
-
Gin关于ProtoBuf的源代码分析原理实现
-
Gin Render机制分析
-
Gin如何优雅的面向接口编程
ProtoBuf数据结构
ProtoBuf的数据结构都是通过.proto
文件进行定义的,然后再通过ProtoBuf工具生成相应平台语言的类库,这样就可以被我们使用了。
// 这是protobuf的版本
syntax = "proto3";
//定义包名
package module;
// 定义数据结构,message 类似golang中的struct
message User {
string name = 1; // 定义一个string类型的字段name, 序号为1
int32 age = 2; // 定义一个int32类型的字段age, 序号为2
}
这就是一个比较简单的ProtoBuf数据结构定义了,和