codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

50 / 72 topics
42Third-Party Libraries Overview43GORM (Object-Relational Mapping)44Gin Framework45Echo Framework46Cobra CLI Library47Viper Config Library48Go Bindata for Embedding Assets49gRPC (Remote Procedure Calls)50Protocol Buffers
Tutorials/Go (Golang)/Protocol Buffers
🐹Go (Golang)

Protocol Buffers

Updated 2026-04-20
3 min read

Protocol Buffers

Protocol Buffers, often referred to as "protobuf," is a language-neutral, platform-neutral, extensible mechanism for serializing structured data. It's designed by Google and used extensively across their systems. In this tutorial, we'll explore how to use Protocol Buffers in Go (Golang), including setting up the environment, defining message types, compiling .proto files, and using generated code.

What is Protocol Buffers?

Protocol Buffers are a way of encoding structured data in an efficient and language-agnostic format. They allow you to define your data structures once and use them across different programming languages and platforms. This makes it easier to maintain consistency between the client and server sides of applications, especially when dealing with complex data types.

Setting Up Protocol Buffers in Go

To start using Protocol Buffers in Go, you need to install the protoc compiler and the Go plugin for protoc.

Step 1: Install protoc

First, download and install the protoc compiler from the official GitHub repository. Choose the appropriate version for your operating system.

For example, on a Unix-like system, you can use:

# Download protoc
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip

# Unzip the file
unzip protoc-3.17.3-linux-x86_64.zip -d protoc3

# Move to bin directory
sudo mv protoc3/bin/* /usr/local/bin/

# Move include directory
sudo mv protoc3/include/* /usr/local/include/

Step 2: Install Go Plugin for protoc

Next, install the Go plugin for protoc using go get:

go get google.golang.org/protobuf/cmd/protoc-gen-go@latest

Ensure that your GOPATH/bin is in your system's PATH. This allows you to run the protoc-gen-go command from anywhere.

Defining Message Types

Protocol Buffers use .proto files to define message types. Here’s an example of a simple .proto file:

syntax = "proto3";

package tutorial;

// Define a Person message type.
message Person {
  string name = 1;
  int32 id = 2;  // Unique ID number for this person.
  string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;
}

Explanation

  • syntax = "proto3";: Specifies the version of Protocol Buffers.
  • package tutorial;: Defines a package name to avoid naming conflicts.
  • message Person { ... }: Defines a message type named Person.
  • string name = 1;: Declares a field with a string type and tag number 1. The tag number is used for encoding the data.

Compiling .proto Files

To generate Go code from your .proto files, use the protoc compiler:

protoc --go_out=. tutorial.proto

This command generates a tutorial.pb.go file in the current directory. This file contains Go code for the defined message types.

Using Generated Code

Here’s how you can use the generated code to create, serialize, and deserialize messages:

Example Code

package main

import (
    "fmt"
    "log"

    pb "path/to/tutorial" // Import the generated package
)

func main() {
    // Create a new Person message.
    person := &pb.Person{
        Name:  "John Doe",
        Id:    1234,
        Email: "john.doe@example.com",
        Phones: []*pb.Person_PhoneNumber{
            {
                Number: "555-4321",
                Type:   pb.Person_HOME,
            },
        },
    }

    // Serialize the message to a byte slice.
    data, err := person.Marshal()
    if err != nil {
        log.Fatalf("Failed to marshal person: %v", err)
    }
    fmt.Printf("Serialized data: %x\n", data)

    // Deserialize the byte slice back into a Person message.
    newPerson := &pb.Person{}
    if err := newPerson.Unmarshal(data); err != nil {
        log.Fatalf("Failed to unmarshal person: %v", err)
    }

    // Print the deserialized message.
    fmt.Printf("Deserialized person: %+v\n", newPerson)
}

Explanation

  • person.Marshal(): Serializes the Person message into a byte slice.
  • newPerson.Unmarshal(data): Deserializes the byte slice back into a Person message.

Best Practices

  1. Use Field Tags Wisely: The tag numbers in your .proto files are used for encoding and decoding. Ensure they remain consistent across different versions of your protocol.
  2. Avoid Breaking Changes: When updating your .proto files, avoid breaking changes that could affect existing clients or servers.
  3. Versioning: Use versioning in your package names to manage different versions of your protocol definitions.

Conclusion

Protocol Buffers provide a powerful and efficient way to serialize structured data in Go. By following the steps outlined in this tutorial, you can set up Protocol Buffers in your Go projects, define message types using .proto files, compile them into Go code, and use the generated code to handle serialization and deserialization.

This guide should give you a solid foundation for working with Protocol Buffers in Go. As you gain more experience, explore advanced features like service definitions and gRPC for building efficient APIs.


PreviousgRPC (Remote Procedure Calls)Next Docker for Go Applications

Recommended Gear

gRPC (Remote Procedure Calls)Docker for Go Applications