Protobuf some things to note

Source: Internet
Author: User
Tags serialization

After reading protobuf Some documents, write something, the right to remember.

Https://github.com/google/protobuf/blob/master/CHANGES.txt:

Google released the Protobuf v3, for PB better use, more cross-language, he made the following changes to PROTOBUF v2:

1. Removal of field presence logic for primitive value fields (inconceivable, retained for later translation), delete required (greatly agree, that is, to preserve repeated, Required and optional are not, the default is optional), delete the default value (do not understand). Google generated these changes in order to better compatible with Android Java, objective C and go language;

2. Deletion of support for the unknown field;

3. Inheritance is no longer supported, with any type of the generation;

4 modifies the unknown type in the enum;

5 support Map;

PROTOBUF v2 and v3 all support map, in the form of the following statements:

Message Foo {

map<string, string> values = 1;

}

Note that the map here is unordered_map.

6 adding a set of types to support presentation time, dynamic data, etc.

7 The default is to encode in JSON form instead of binary.

Currently the V3 Alpha version only implements 1-5 of these five feature,6 and 7 is not yet supported. A new syntax keyword is added to indicate the PROTOBUF protocol version of the proto file, and v2 is not specified. Such as:

Foo.proto

Syntax = "Proto3";

Message Bar {...}

If you are currently using V2, you will not be able to support your switch to V3 for the time being, and we'll also provide support for V2. If you are a novice, then use the V3 boldly.



HTTPS://GITHUB.COM/GOLANG/PROTOBUF:

The 1 Go PROTOBUF implementation does not support RPC.

The 2 Go protobuf implements a go plugin protoc-gen-go, where he has to place the $gobin inside and default to $gopath/bin. It must also be inside the $path to allow the Protoc compiler to find it. Protoc the proto file into the go source file, whose name suffix is. PB.GO,PROTOC command format as follows:

Protoc--go_out=. *.proto

3. Some of the Pb.go source code matters are as follows:

-variable names are adopted by camel nomenclature, such as Camel_case is compiled into CamelCase.

-The Set method is not generated for field and is assigned directly to the member.

-There is no setter, but there is a getter method, and if the field is set to a value, the set value is returned. If it is not set, the default value is returned. If even the message is not received, return to nil.

-All member initial values for struct are 0 values, and if you want to assign a value to its members, you must precede the serialization. It does not make sense to modify the struct member values after serialization.

-Struc Reset () will clear the value of all field values of struct.

-the type of a repeated field member is a pointer type, and when it is empty, it means that its value is empty. The compiled type of "required field int32 F" or "optional field int32 F" is f *int32.

-The fields of the repeated type is compiled slices.

-Like other languages, go generates helper functions to set the value of the field.                        It is no longer recommended for getting a worthwhile helper function. Msg. Foo = Proto. String ("Hello")//Set field

-a field if there is a default value, the value is compiled to a constant whose name is default_structname_fieldname and the associated Get method returns the value by default. Do not use these const values directly.?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 Message Test {optional Int32 type = 2 [

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.