Skip to content

Today I learned

Learn something new every day

Menu
  • Home
  • About
  • Hello World! and Why I Started This Blog
Menu

What is Protobuf and why use it

Posted on 06/02/202205/02/2022 by Sagi Rosenthal

Today I learned what is protobufs. “Protocol Buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data”. Or, in human language, it is a set of rules of what data of a certain kind looks like.

In many languages, there are primitive data types such as int32, string, boolean, array (often called as list or slice), and map (of key-value). Some languages define a way to create more complex and user-defined data types.

type Computer struct {
    NumCpuCores    int
    RamSize        int
    HaveWifi        bool
    Devices        []string
    PartitionSize  map[string]int
}
struct product {
    int             num_cpu_cores;
    double          ram_size;
    bool            have_wifi;
    string          devices[256]
    map<string,int> partition_size  
} ;

Why is that important? sometimes we want a set of different kinds of values of a specific type to pass together. In the example above we define what info about a computer looks like. But how it is related to profobufs? – well, a protobuf is a common way to define how data will look like when it is used in multiple languages, say you have a client written in Go and a server written in Java. Using the protocol buffers a client can serialize (transfer the data from its multi-type variables to binary data) the go struct and send it as bytes to the server, whereas the server can de-serialize it and read it in Java language.

message Computer {
    int32 NumCpuCores = 1;
    int64 RamSize = 2;
    bool HaveWifi = 3;
    repeted string Devices = 4;
    map<>string,int32> PartitionSize = 5;
}

Protobuff has a lot more interesting features, such as:

  • serialize can compress the size of the data
  • highly configurable fields options (onof fileds, optional/required, tagging, etc..)
  • rpc definitions
  • and more

Check out Google’s protocol-buffers page: https://developers.google.com/protocol-buffers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CAPTCHA


Recent Posts

  • How to Sell Stuff Online
  • How to Make Your Kubernetes Dev Cycle Faster
  • How To Be A Valuable Team Member
  • Don’t Make, Go Generate It
  • Why You Should Monkey Patch Your Next Unit-Test

Archives

  • July 2023
  • April 2023
  • January 2023
  • November 2022
  • August 2022
  • July 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Go
  • Learning
  • Software Utilities
Join the updates channel
© 2026 Today I learned | Powered by Minimalist Blog WordPress Theme