protobuf 嵌套示例

本文详细介绍了Google Protocol Buffers的使用方法,包括嵌套消息、导入消息的概念以及动态编译等内容。通过实例展示了如何定义、导入并使用消息类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.嵌套 Message

message Person

{

required string name = 1;

required int32 id = 2;        // Unique ID number for this person.

optional string email = 3;

enum PhoneType

{

MOBILE = 0;

HOME = 1;

WORK = 2;

}

message PhoneNumber

{

required string number = 1;

optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

}

2. Import Message

在一个 .proto 文件中,还可以用 Import 关键字引入在其他 .proto 文件中定义的消息,这可以称做 Import Message,或者 Dependency Message

import common.header;

message youMsg

{

required common.info_header header = 1;

required string youPrivateData = 2;

}

Import Message 的用处主要在于提供了方便的代码管理机制,类似 C 语言中的头文件。您可以将一些公用的 Message 定义在一个 package 中,然后在别的 .proto 文件中引入该 package,进而使用其中的消息定义。Google Protocol Buffer 可以很好地支持嵌套 Message 和引入 Message

3.更多关于动态编译的内容,参数[1]

原文

[1]http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/

[2]https://developers.google.com/protocol-buffers/docs/overview?hl=zh-CN

[3]https://developers.google.com/protocol-buffers/docs/cpptutorial?hl=zh-CN

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值