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:
- Install the protoc command-line tool and the proto library.
- Write a protobuf file with the .proto extension.
- Compile the file so that it targets a programming language (in our case, it is Go).
- Import structs from the generated target file and add the necessary data.
- Serialize the data into binary format and send...