How to use Protobuf with Go?

Table Of Contents

  • Prerequisites
  • Protobuf
  • Bootstrapping Project
  • Creating Protobuf Messages
  • Publishing a Message
  • Consuming a Message
  • Conclusion



  1. With these Proto Messages, you will know what type of data you are sending & receiving
  2. Serves as a documentation
  3. Supports validation for serializing & deserializing
  4. Proto messages are language-neutral and it supports various programming languages which means you can publish messages from Go & start consuming them from Php / Node etc

Bootstrapping Project

  • Copy Dockerfile & docker-compose.yaml from this repository which will dockerized Go source code, Proto compiler & RabbitMQ
  • Create a file publish.go where you will write some code to publish events
  • Create a file consume.go where you will write some code to consume events

Creating Protobuf Messages

  1. Create a folder messages and inside that folder create a file called user_registered.go
  2. Inside user_registered.go copy the following codes. Let's understand the code
  • syntax defines which proto version you are going to use
  • go_package defines the path where all your generated Protobuf code will be into
  • `import “validate/validate.proto”; it is importing the validator so that you can validate message using various rules without writing it by yourself
  • All message starts with a message keyword followed by name of the message. Inside the block, you can write key-value pairs where the key defines name & type & the value defines its order & rules if there are any. In our case, you are defining the first field userId which is an integer and should be greater than 0 and the next is an email which is a string and should not be empty.

Publishing a Message

  1. Create a directory pkg and a file rabbitmq.go inside pkg dir where you will make a connection with RabbitMQ. The following code the function getConnection is importing library and making a connection with RabbitMQ server and upon error function failOnError is terminating the application with an error.

Consuming a Message

  1. Inside rabbit. go add a function that will help in consuming a message. The following code makes a connection to RabbitMq & returns a channel that you can leverage to listen to incoming messages.
  • The main function is listening to message by creating a consuming client. While deserializing message it is importing UserRegistered struct from the auto-generated code to define the type of data. There might be cases where the producer might send invalid data due to various reasons so it is better to validate that data you are consuming are as per the contracts that you specified.
  • validateMessage accepts the JSON data and tries to validate as per the user_registered.proto definition and if the data breaks any validation rules it will throw a validation error





Software Development Enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

This week in #Scala (Nov 29, 2021)

Using namespaces on HAProxy to segregate your traffic

The forgotten agile

Covering my tracks in 2018

Always This Lean Thing — I Mean, What Is It Actually? Something To Do With Six Sigma?

What are Constructors in Java

Stop using the environment variables for sensitive keys in Rails

DM Software Pathfinder project update #4

DM Software Pathfinder project update number 4, covering 16 March to 11 April 2022

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
ujwal dhakal

ujwal dhakal

Software Development Enthusiast

More from Medium

Concatenating strings in GO- Performance comparison between different techniques

Go-ing Serverless: How to Write Efficient Go Programs

Range Over Slice — Golang Snippet

3 ways to tackle the longest increasing subsequence problem(Golang)