google Protocol Buffer 入门

本文介绍了Google Protocol Buffer,一种轻便高效的结构化数据存储格式,适用于数据串行化和RPC数据交换。主要内容包括ProtoBuf的简介、入门步骤、字段规则(required、optional、repeated)的解释,以及如何编译.proto文件并编写读写程序。
摘要由CSDN通过智能技术生成

来源于我的个人博客, 原文用markdown编写,转到csdn格式有些问题。

简介

Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 个 .proto 文件。他们用于 RPC 系统和持续数据存储系统。
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。

入门

下载安装protobuf后编写hello.proto文件

     
     
     
1
2
3
4
5
6
7
     
     
     
package lm;
message helloworld
{
required int32 id= 1;
required string str= 2;
optional int32 opt= 3;
}

.proto文件定义需要进行序列化和反序列化数据的格式。具体参考官方文档。其中的required,optional的官方说明如下

Specifying Field Rules

You specify that message fields are one of the following:

required:

a well-formed message must have exactly one of this field.

optional:

a well-formed message can have zero or one of this field (but not more than one).

repeated:

this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.

定义中的

     
     
     
1
     
     
     
required int32 id= 1;

1是id在message helloword中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值