目录
-
1.类型
-
2.VARINT
-
2.1 无符号数
-
2.2 有符号数
-
-
3.定长
-
3.1 I64类型
-
3.2 I32类型
-
-
4.LEN
-
5.代码
学习这些有什么用?
-
如果你是后端开发者,掌握这个对工作非常有用
-
如果你是求职者,面试时可以临危不惧
1.类型
最近看到有直接操作wire type相关的api调用,例如:
-
WriteTag
-
WriteVarint32
等等,那么什么是wire type以及protobuf最核心的编码原理是什么?
ID | Name | Used For |
---|---|---|
0 | VARINT | int32, int64, uint32, uint64, sint32, sint64, bool, enum |
1 | I64 | fixed64, sfixed64, double |
2 | LEN | string, bytes, embedded messages, packed repeated fields |
3 | SGROUP | group start (deprecated) |
4 | EGROUP | group end (deprecated) |
5 | I32 | fixed32, sfixed32, float |
如上表,官方提供了六个wire类型,其中SGROUP与EGROUP废除掉了,我们忽略它,来看看其他4个编码。

本篇文章内容与代码已更新与知识星球,感兴趣的加入即可,欢迎一起讨论。
