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

Modelling Dynamic Systems in Python

ArgParse as a tool for tracking Machine Learning hyperparameters?

Deconstructing Agile

Exploiting the Benefits of Reasoning & Inference using various RDF Frameworks and OpenLink Virtuoso…


In Focus: Deepu Thomas Philip

Creating Menu using Python Automation

Java — 25+ years of evolution and the lifecycle ahead

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

NSQ Go — Error E_FIN_FAILED FIN xxx failed ID not in flight

State of Golang linters and the differences between them — SourceLevel

How To Upgrade An Indirect Dependency In GoLang

How to create a CLI in Golang