How to use Protobuf with Go?

Table Of Contents

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


This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:


Protobufs are the Messaging Contracts. They are schema that describes the structure of the message. These files are written in .proto extension which is language-neutral i.e write once & use anywhere. Proto Messages are the structured schema that is inside .proto files

  1. Serves as a documentation
  2. Supports validation for serializing & deserializing
  3. 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

For creating a new test project follow the steps below:

  • 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

In an application, while you sending messages you will serialize data using the definition from Proto Messages. And when consuming messages you will deserialize them using the same Proto Messages. Let's create a Proto Message so your services can use them to send and receive messages.

  1. Inside user_registered.go copy the following codes. Let's understand the code
  • 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

As you have successfully created a Proto Message. Let's use the same auto-generated code while sending messages to other services for this you need to write a way to publish messages. Follow the following steps:

Consuming a Message

Until now you defined a photo message then you created a rabbitmq.go to create a way to publish messages & you used the same function publish.go to publish messages to RabbitMq. Now let's consume the message that you published earlier.

  • 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


Throughout this tutorial, you have learned Protobuf & how to use them with GO & RabbitMQ.



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