Creating your first protocol buffer
Protocol buffers are a flexible, efficient, and automated mechanism for encoding and serializing structured data supported by Go. In this recipe, we will learn how to write our first protocol buffer.
Getting ready…
- Verify whether
protocis installed by executing the following command:
$ protoc --version libprotoc 3.3.2
- Install
protobufby way of the following:
$ git clone https://github.com/google/protobuf $ cd protobuf $ ./autogen.sh $ ./configure $ make $ make check $ make install
How to do it…
- Create
hello.protoinside theprotodirectory and define aserviceinterface with the nameSay, which has two datatypes—RequestandResponse, as follows:
syntax = "proto3";
service Say
{
rpc Hello(Request) returns (Response) {}
}
message Request
{
string name = 1;
}
message Response
{
string msg = 1;
}- Compile
hello.protowith the following command:
$ protoc --go_out=plugins=micro:. hello.protoHow it works…
Once the command has executed successfully, hello.pb.go...