Protocol Buffer Implementation in Golang

What is Protocol Buffer?

Protocol Buffer is a software framework for serializing structured data. It is a binary format and is used to serialize and deserialize data. It is used to serialize and deserialize data in the form of messages. One schema defines the structure of the data and the other schema defines the structure of the message.

Why is there a need for Protocol Buffer?

Protocol Buffer is used to serialize and deserialize data. It is used to serialize and deserialize data in the form of messages. One schema defines the structure of the data and the other schema defines the structure of the message. The schema defines the structure of the data and the message determines the structure of the data.

What is schema in Protocol Buffer?

Schema is a definition of the data structure. It is used to define the structure of the data. In Protobuf, the schema is defined in the .proto file. The .proto file is a text file. The .proto file is used to define the structure of the data.

Primarily we use Protocol Buffer messages/data to communicate between microservices in an encrypted way.

Now let's understand with some examples:

In order to work with the Protocol Buffer we need to install the following packages:

go get -u google.golang.org/protobuf/proto

Now will create a proto file with a simple message schema:

syntax = "proto3";package main;option go_package = "/;msgmodel";message SimpleMessage {
string Name = 1;
string Email = 2;
int32 Age = 3;
bool IsAProgrammer = 4;
}

To get more information about the protobuf example you can visit the golang protobuf example repository given below:

https://github.com/iyashjayesh/Protobuf-Golang-Example/

The following command will generate the protobuf files that we can use in our application:

protoc --go_out=. ./*proto

We need to first structure the data we want to send. Then we need to create a protobuf message and encode the data using the protobuf package.

As shown in the following code,

package mainimport (
"log"
"golang-protobuf-example/msgmodel""google.golang.org/protobuf/proto"
)
func main() {log.Println("ProtoTest")Message1 := &msgmodel.SimpleMessage{
Name: "Yash Chauhan",
Email: "testmail@gmail.com",
Age: 30,
IsAProgrammer: true,
}
log.Println("Message1 Name: ", Message1.Name)
log.Println("Message1 Email: ", Message1.Email)
log.Println("Message1 Age: ", Message1.Age)
log.Println("Message1 IsAProgrammer: ", Message1.IsAProgrammer)
data, err := proto.Marshal(Message1)// Unmarshal the data into the struct
var Message2 msgmodel.SimpleMessage
err = proto.Unmarshal(data, &Message2)
if err != nil {
log.Println("Error: ", err)
}
log.Println("Message2 Name: ", Message2.Name)
log.Println("Message2 Email: ", Message2.Email)
log.Println("Message2 Age: ", Message2.Age)
log.Println("Message2 IsAProgrammer: ", Message2.IsAProgrammer)
}

https://github.com/iyashjayesh/Protobuf-Golang-Example/blob/main/main.go

Clone this repository to your local machine and try it out.

Make sure to give a star to the repository. It’ll motivate me to create more tutorials like this.

Just for knowledge:

To make the field array or list we have to make the use of repeated before initializing the variable, as shown in the below example:

message CompanyXYZ{
repeated string Name
}
  • Required fields are not allowed in proto3.
  • Field labels (required/optional/repeated) are not allowed on map fields.

https://stackoverflow.com/questions/31801257/why-required-and-optional-is-removed-in-protocol-buffers-3

I hope you find it helpful. If you have any suggestions, please feel free to drop a comment.

Thanks for reading! A few (hopefully 50) claps? are always appreciated. Follow me and share this article if you like it.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store