Compiling a protocol buffer with protoc
Until now, we have discussed how to write a protocol buffer file that is previously written in JSON or another data format. But, how do we actually integrate it into our programs? Remember that protocol buffers are data formats, no more than that. They are a format of communication between various systems, similar to JSON. These are the practical steps we follow for using protobufs in our Go programs:
- Install the
protoc
command-line tool and theproto
library. - Write a protobuf file with the
.proto
extension. - Compile it to target a programming language (here, it is Go).
- Import structs from the generated target file and serialize the data.
- On a remote machine, receive the serialized data and decode it into a struct or class.
Take a look at the following diagram:
The first step is to install the protobuf
compiler on our machine. For this, download the protobuf
package from https://github.com/google/protobuf/releases. On macOS X, we can install protobuf
using...