About Protocol Buffers & Generate go models using Protobuf

jafar
3 min readApr 11, 2021

Introduction

Nowadays microservices are playing a major role in the market. if one service wants to connect with another service that case we will make HTTP connections to transfer the data or get the data. HTTP protocol uses JSON-type data. JSON data has many obvious advantages like human readable, well understood, and typically performs well. but in the case of internal requests, these features are not required.

So Google developed Protocol Buffers usually referred to as Protobuf. Protobuf uses binary encoding. they focused on making it simpler, smaller, faster, and more maintainable than XML. But, this protocol even surpassed JSON with better performance, better maintainability, and smaller size.

Some advantages before going into steps.

  • schemas are very simple. it uses a binary encoding format that allows you to specify a schema. protocol buffers associate data types with field names, using integers to identify each field.
protocol buffer schema
  • Easy language interoperability. we can use the same schema to generate different language models.
#go-lang model
protoc --go_out=.*proto
#java-lang model
protoc --java_out=.*proto
  • Easy backward compatibility. we can easily convert from a new version data model to an old version data model. for example if you observe the below models both have three elements but if we do JSON unmarshal with new version data with the old version data model, it will fail but if we use the protocol buffers it will convert new-version data model to old-version data model. because it uses binary encoding and it uses the integers to identify each field.
Easy backward compatibility
  • Code Reusability. for example for we want to print the mobile number field from the below model. we have to check the model nil pointer if data != nil { if data.details != nil then print data.details mobile } } otherwise code will break. but in the case of Protobuf, it will give the predefined method GetDetailsMobile(). let's assume n no. of functions trying to access the mobile field everywhere no need to add nil pointer check conditions. just calling GetDetailsMobile() is enough.

Protocol Buffer installation process

installation steps

Steps to generate models

  • Provide a version of the protocol buffer.
  • package name ( path to package)
  • Provide Model Schema.
schema.proto
protoc --go_out=. *.proto

This will generate the schema.pb.go file in src/models dir.

src/models/schema.pb.go

if you see structure it has more #elements inside a structure, these are not defined in the proto schema. protoc-go-gen explicitly added these elements. to removing these extra elements we have to use the gogo-faster plugin. gogo-faster plugin removes the extra elements and it will make the marshal and unmarshalling faster. take a look at the below example schema.

gogo-faster plugin example schema(schema.proto)
#protoc -I <path to proto files dir> -I <path for the gogo-faster pulgin dir> --gogofaster_out=. <all proto files> $ protoc -I ./ -I src/ --gogofaster_out=. *.proto
schema.pb.go

That’s All Folk’s

for more info refer to docs:

https://developers.google.com/protocol-buffers/docs/gotutorial

--

--