Google Protobuf Primer (2) Language Guide

Google Protobuf Primer (2) Language Guide

去年年初初次接触 Google Protobuf,如今已经有不少变化,从这篇开始,续一下 :)

1. 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.

For historical reasons, repeated fields of basic numeric types aren't encoded as efficiently as they could be. New code should use the special option [packed=true] to get a more efficient encoding. For example:

repeated int32 samples = 4 [packed=true];

But when you use it as the following:

message Locations {
    repeated Location location = 1 [packed=true];
}

compile it, then you will got a warning:

[packed = true] can only be specified for repeated primitive fields.

So pay attention to the type of fields you intend to specify.

2. Scalar Value Types

proto TypeNotesC++ TypeJava Type
double doubledouble
float floatfloat
int32variable-len, inefficient for negative numint32int
int64variable-len, inefficient for negative numint64long
uint32variable-lenuint32int
uint64variable-lenuint64long
sint32variable-len, signed numint32int
sint64variable-len, signed numint64long
fixed32always 4-byte, efficient > 228int32int
fixed64always 8-byte, efficient > 256int64long
sfixed32always 4-byteint32int
sfixed64always 8-btyeint64long
bool boolboolean
stringUTF-8 string, 7-bit ASCII stringstringString
bytesany arbitrary sequence of bytesstringByteString

概括下:

  • 可正可负,范围小(228或256以内):sint32 或 sint64
  • 可正可负,范围大(228或256以上):sfixed32 或 sfixed64
  • 只会正的,范围小(228或256以内):uint32 或 uint64
  • 只会正的,范围大(228或256以上):fixed32 或 fixed64
  • 布尔:bool
  • 浮点:float 或 double
  • utf8 string 或 7-bit ascii string:string
  • ByteString:bytes

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant,微博:weibo.com/lauginhom

-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值