多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## Getting Started There are several ways to use gogoprotobuf, but for all you need to install go and protoc. After that you can choose: Speed More Speed and more generated code Most Speed and most customization ## Installation To install it, you must first have Go (at least version 1.6.3) installed (see http://golang.org/doc/install). Latest patch versions of Go 1.8 and 1.9 are continuously tested. Next, install the standard protocol buffer implementation from https://github.com/google/protobuf. Most versions from 2.3.1 should not give any problems, but 2.6.1, 3.0.2 and 3.4.0 are continuously tested. ## Speed Install the protoc-gen-gofast binary go get github.com/gogo/protobuf/protoc-gen-gofast Use it to generate faster marshaling and unmarshaling go code for your protocol buffers. protoc --gofast_out=. myproto.proto This does not allow you to use any of the other gogoprotobuf extensions. ## More Speed and more generated code Fields without pointers cause less time in the garbage collector. More code generation results in more convenient methods. Other binaries are also included: protoc-gen-gogofast (same as gofast, but imports gogoprotobuf) protoc-gen-gogofaster (same as gogofast, without XXX_unrecognized, less pointer fields) protoc-gen-gogoslick (same as gogofaster, but with generated string, gostring and equal methods) Installing any of these binaries is easy. Simply run: ``` go get github.com/gogo/protobuf/proto go get github.com/gogo/protobuf/{binary} go get github.com/gogo/protobuf/gogoproto ``` `注: binary 代指proto-gen-xxx 所有` ## Most Speed and most customization Customizing the fields of the messages to be the fields that you actually want to use removes the need to copy between the structs you use and structs you use to serialize. gogoprotobuf also offers more serialization formats and generation of tests and even more methods. Please visit the extensions page for more documentation. Install protoc-gen-gogo: ``` go get github.com/gogo/protobuf/proto go get github.com/gogo/protobuf/jsonpb go get github.com/gogo/protobuf/protoc-gen-gogo go get github.com/gogo/protobuf/gogoproto ``` ## gogoprotobuf的各个option 1 gogoproto.goproto_enum_prefix 选项为false,生成的代码中不加"E_"。 2 gogoproto.goproto_getters 选项为false,不会为message的每个field生成一个Get函数。 3 gogoproto.face 选项为true的时候,为message生成相应的interface。 4 gogoproto.nullable nullable这个option违背protobuf的初衷。使用它,message序列化后,gogo为message的每个field设置一个值,而google protobuf则是要求如果一个option的field没有被赋值,则序列化的时候不会把这个成员序列化进最终结果的。 5 gogoproto.customname field的名称与message的method的名称一样。 还有gogoproto.customtype 6 gogoproto.marshaler gogoproto.sizer gogoproto.marshaler_all gogoproto.sizer_all sizer选项true,gogo会相应的message生成"func Size() int";marshaler为true,gogo为相应的生成:func Marshal()([] byte, int),这个 method会调用Size(),所以marshaler为true的时候,sizer也必须为true。 option (gogoproto.marshaler_all) = true; option (gogoproto.sizer_all) = true; 7 gogoprotobuf.unmarshaler & gogoprotobuf.unmarshaler_all unmarshaler为true,gogo为相应的message生成func Unmarshal(data []byte) error,代替goprotobuf的proto.Unmarshal([]byte, *struct)函数 option (gogoproto.unmarshaler_all) = true;