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 Protocol Buffers format. Then, we will use code generation tools alongside Protocol Buffers to generate our data structures. Finally, we will illustrate how to use our generated code to serialize and deserialize our data.
First, let’s prepare our application. Create a 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 {
double rating = 1;
Metadata metadata = 2;
}
In the first line of our schema definition, we set the syntax to proto3
, the latest version...