Protocol Buffers introduce new Go API, and support for older versions indefinitely

Go team announced the launch of  Protocol Buffers  new Go API (APIv2), and that this is a significant revision. APIv2 main provider of reflection (Reflection) function, through which the developer is able to detect numerical Protocol Buffers type system, but because of the change of the official Message type definitions, and therefore is not compatible with the old and new version of the API.

Protocol Buffers is Google developed language-independent data exchange format, the sequence of the extension mechanism is structured data, and XML is similar, but smaller and simpler.

Construction of new API's motivation

Go's first Protocol Buffers package has been released ten years , in this decade, this package together with the development and expansion of Go, its users demand has also increased. Go official mentioned that many developers want to write using reflection (Reflection) program to check the protocol buffer (protocol buffer) message, but reflectreflected only the package can detect the type and value of Go, the system ignores the agreement from the buffer type the information. For example, we might want to write a play through log entries and remove any comments for the field containing sensitive data function, but is not part of the comment field Go type system, so the operation is too much trouble.

In addition, developers can use data structures other than the structure of the protocol data buffer generated by the compiler, which can represent, for example, the message type is unknown at compile-time type feeds. Finally, proto.Messageit is also a common source of the problem.

The official said that these three issues are a common cause, there is also a common solution: Messagethe interface should be fully specify the behavior of the message, and to Messagevalue function operation should be free to accept any type correctly implement the interface.

Because Messageyou can not change the existing definition of the type of package, while maintaining API compatible, so the official decision to launch a new Protocol Buffers module, and the new version is not compatible with older versions.

Reflection (reflection) is the new version of the flagship feature, which provides similar reflectfunctionality and value of the reflection type package detection of Go. Protocol Buffers ability of reflection function according to the type of Protocol Buffers, providing the ability to detect the value of the system, the package contains a descriptor type, the source document describes the structure and type of the value defined in the interface, which provides operating and checking messages .

The official version of the original Go Protocol Buffers called APIv1, and the new version called APIv2, due to incompatibility APIv1 and APIv2, so use a different path for each module version. The official noted, because each developer to migrate to new versions of different speeds, and some programs may continue to use the old version, even within the same program, there are different versions may use the API, and therefore intends to present the official indefinitely support API v1.

Guess you like

Origin www.oschina.net/news/113960/a-new-go-api-for-protocol-buffers