Compiling a protocol buffer with protoc

So far, we have discussed how to write a protocol buffer file by defining messages and their field types. But how do we actually integrate one into our Go programs? Remember that protobufs are a format of communication between various systems, similar to JSON. But the actual data that is transferred is binary. The protoc compiler automatically generates Go structs from .proto files. Later, those structs can be imported to create binary data.

The following are the practical steps we follow when using protobufs in our Go programs:

  1. Install the protoc command-line tool and the proto library.
  2. Write a protobuf file with the .proto extension.
  3. Compile the file so that it targets a programming language (in ...

Get Hands-On RESTful Web Services with Go - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.