Using Protocol Buffers
In this section, we are going to illustrate how you can use Protocol Buffers for your applications. We will use the microservice examples from the previous chapters and define our data model in the Protocol Buffers format. Then, we will be using the code generation tools with Protocol Buffers to generate our data structures. Finally, we will illustrate how to use our generated code for serializing and deserializing our data.
First, let’s prepare our application. Create the directory called api
under our application’s src
directory. Inside this directory, create a movie.proto
file and add the following to it:
syntax = "proto3";
option go_package = "/gen";
message Metadata {
string id = 1;
string title = 2;
string description = 3;
string director = 4;
}
message MovieDetails {
float rating = 1;
...