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
protoc
is installed by executing the following command:
$ protoc --version libprotoc 3.3.2
- Install
protobuf
by 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.proto
inside theproto
directory and define aservice
interface with the nameSay
, which has two datatypes—Request
andResponse
, as follows:
syntax = "proto3"; service Say { rpc Hello(Request) returns (Response) {} } message Request { string name = 1; } message Response { string msg = 1; }
- Compile
hello.proto
with the following command:
$ protoc --go_out=plugins=micro:. hello.proto
How it works…
Once the command has executed successfully, hello.pb.go...