Protobuf and Protoc-gen-go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. # # What is Protobufprotobuf (Protocol Buffer) is a data interchange format for Google, which is independent of the language and platform-independent. Google offers implementations in multiple languages: Java, C #, C + +, go and Python, each of which contains compilers and library files for the appropriate language. Because it is a binary format, it is much faster than using XML for data exchange. It can be used in data communication between distributed applications or in heterogeneous environments. As a good efficiency and compatibility of the binary data transmission format, can be used for such as network transport, configuration files, data storage and many other areas. (see [link] (Https://baike.baidu.com/item/protocol%20buffer/1664400?fr=aladdin)) # # What is Protocprotoc is the compiler for the Protobuf file (. Proto) (refer to [link] (HTTPS://GITHUB.COM/GOOGLE/PROTOBUF)), which can be used by this tool. Proto Files translated into various programming languages corresponding to the source code, including data type definition, call interface and so on. [PROTOC Compilation process] (https://static.studygolang.com/180325/41775c725a2745e2e785f22bbd96a08c.png) by viewing the source code of the PROTOC (see [GitHub Library] (https:// GITHUB.COM/GOOGLE/PROTOBUF) can be known, PROTOC in the design of protobuf and different language decoupling, the underlying use C + + to achieve the PROTOBUF structure of the storage, and then through the form of plug-ins to generate different language source code. The PROTOC compilation process can be divided into two simple steps (as shown): 1) parsing. Proto files, translating native data structures into protobuf in memory, 2) passing PROTOBUF related data structures to the corresponding language's compiled plug-ins, which are responsible for the PROTOBUF native structure received by the plug-in. Renders the template for the output-specific language. The plugins included in the source code (see [GitHub Library] (https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/main.cc)) have CSharp, Java, JS, Objectivec, PHP, Python, Ruby and many more. ## What is Protoc-gen-go protoc-gen-go is the go version of the protobuf** compilation plugin * * series. You know from the previous section that the native PROTOC does not include the go version of the plugin, but you can find a dedicated code base on GitHub (see [GitHub Library] (github.com/golang/protobuf/protoc-gen-go)). Since Protoc-gen-go is written by go, it is easy to install it and only need to run ' go get-u github.com/golang/protobuf/protoc-gen-go ' to be able to $gopath/ This tool is found in the bin directory. At this point, you can use protoc-gen-go with the following command. "' Shellprotoc--go_out=output_directory Input_directory/file.proto" where "--go_out=" means to generate a go file, PROTOC will automatically look for the protoc-gen-go execution file in path (System execution path). # # Protoc-gen-go The source! [Protoc-gen-go's source structure] (https://static.studygolang.com/180325/fcfa10f6aa545027a3904783f0fd6ab5.png) According to the code style of Go, Protoc-gen-go source code mainly consists of six packages (package): * Main Packages * Doc.go is mainly described. * Link_grpc.go explicitly references the PROTOC-GEN-GO/GRPC package, triggering the init function of GRPC. * Main.go code less than 50 lines, initialize generator, and call generator corresponding method output protobuf go language file. * Generator Package * Generator.go contains most of the rendering methods of the PROTOBUF native structure to the Go language file, where ' func (g *generator) P (str ... interface{}) ' This method will output the render to generator (generator anonymously nested bytes. Buffer, so there is a method of buffer). * Name_test.go test, mainly includes the test of the name-related method in generator. * GRPC Package * Grpc.go is similar to generator, butContains many methods for generating GRPC related methods, such as the RPC method defined in render translation protobuf (not included in generator, which does not translate the definition of service by default) * Descriptor A description file containing the PROTOBUF (. proto file and its corresponding go compilation file), where the proto file is from the proto library (see [Here] (https://github.com/google/protobuf/blob/master/ Src/google/protobuf/descriptor.proto) * Plugin contains plugin description files (. proto files and their corresponding go-compiled files), where proto files are from the proto Library, see [Here] ( Https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/plugin.proto) # # Epilogue from The Legend of Babel (see [Here] ( Https://baike.baidu.com/item/%E5%B7%B4%E5%88%AB%E5%A1%94/67557?fr=aladdin), it is important to understand that communication between individuals is essential to building large systems. The advent of PROTOBUF provides a language specification for connections between different systems, so long as this specification is followed, the decoupling between the systems is well suited to the popular microservices architecture in recent years. If Protoc and protoc-gen-go as two micro-services, you can find that the two services are completely decoupled, they are fully responsible for different functions, can be coded separately, upgrade, the two services are connected to the proto specification. 300 reads  ∙  1 likes  
Related Article

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.