Protobuf

protobuf 使用

Posted by nomadli on November 12, 2018

source

tips

  • 没有数据长度,需要自己包一层

gen

  • protoc –go_out=. *.proto //cpp_out csharp_out java_out js_out objc_out ruby_out

proto3

syntax = "proto3";
package xxxx; //for golang
double float int32 int64 sint32 sint64 fixed32 fixed64 sfixed32 sfixed64 bytes map
//import "google/protobuf/any.proto";
enum Type {
    reserved 3, 4, 9 to 40;
    reserved xxxx;
    xxxx = 0;
    xxxx = 1;
}
message Phone {
    Type type = 1;
    string number = 2;
}
message Person {
    int32 id = 1;
    string name = 2;
    repeated Phone phones = 3;
}
  • c++
    • MessageLite 不包含描述及反射的版本 在pb中option optimize_for = LITE_RUNTIME;指定
    • Message 继承MessageLite,包含描述及反射