Go Engineer Comprehensive Course: Protobuf Guide [Study Notes]

Protocol Buffers Getting Started Guide 1. Introduction Protocol Buffers (protobuf for short) is a language-agnostic, platform-agnostic, extensible structured data serialization mechanism developed by Google. Compared with serialization methods such as JSON and XML, protobuf is smaller, faster, and simpler. Project homepage: https://github.com/protocolbuffers/prot…

Protocol Buffers Getting Started Guide

1. Introduction

Protocol Buffers (protobuf for short) is a language-agnostic, platform-agnostic, extensible mechanism for serializing structured data developed by Google. Compared to serialization methods like JSON and XML, protobuf is smaller, faster, and simpler.

2. Installation

2.1 Install protoc Compiler

The protoc compiler is used to compile .proto files into source code for specific programming languages.

  1. Download the pre-compiled version suitable for your platform from https://github.com/protocolbuffers/protobuf/releases.
  2. After decompression, copy the protoc executable from the bin directory to a directory included in your system's PATH environment variable (e.g., /usr/local/bin).
  3. Verify installation:

bash
protoc --version

2.2 Install protobuf Runtime Environment

Different programming languages require the installation of their respective protobuf runtime environments. For example, for Go language:

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

Ensure that $GOPATH/bin is added to your system's PATH environment variable so that protoc can find the protoc-gen-go plugin.

3. Defining Message Types

Create a .proto file to define the data structure. For example, create student.proto:

syntax = "proto3";

package main;

message Student {
  string name = 1;
  bool male = 2;
  repeated int32 scores = 3;
}

Execute the following command in the current directory to generate Go language code:

protoc --go_out=. student.proto

This will generate the student.pb.go file, which contains the Go language implementation of the Student type.

4. Using Generated Code

In Go language, the generated code can be used for data serialization and deserialization.

package main

import (
  "fmt"
  "log"

  "google.golang.org/protobuf/proto"
)

func main() {
  student := &Student{
    Name:   "Alice",
    Male:   false,
    Scores: []int32{90, 95, 100},
  }

  // 序列化
  data, err := proto.Marshal(student)
  if err != nil {
    log.Fatal("Marshaling error: ", err)
  }

  // 反序列化
  newStudent := &Student{}
  err = proto.Unmarshal(data, newStudent)
  if err != nil {
    log.Fatal("Unmarshaling error: ", err)
  }

  fmt.Println("Student:", newStudent)
}

5. Field Rules

  • optional: Indicates that the field is optional; it can be set or left unset. If not set, it will be initialized with its default value.
  • repeated: Indicates that the field can be used any number of times, similar to a dynamic array.

Note: In proto3, the required keyword has been removed, and all fields are optional by default.

6. Enums and Nested Types

You can define enum types and nested message types in a .proto file. For example:

syntax = "proto3";

package main;

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;

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

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

  repeated PhoneNumber phones = 4;
}

7. Backward Compatibility

When updating .proto files, to maintain backward compatibility, you should follow these rules:

  • Do not change the field numbers of existing fields.
  • Do not delete existing fields.
  • You can add new fields, but they must use new numbers.
  • You can add new enum values.

Following these rules ensures that older code can still parse new message formats.

8. More Resources

主题测试文章,只做测试使用。发布者:Walker,转转请注明出处:https://walker-learn.xyz/archives/4460

(0)
Walker的头像Walker
上一篇 Apr 20, 2025 19:12
下一篇 Nov 25, 2025 16:00

Related Posts

  • In-depth Understanding of ES6 011 [Learning Notes]

    Promises and Asynchronous Programming

    Because the execution engine is single-threaded, it needs to track the code that is about to run. This code is placed in a task queue. Whenever a piece of code is ready to execute, it is added to the task queue, and whenever a piece of code in the engine finishes execution, the event loop executes the next task in the queue. A Promise acts as a placeholder for the result of an asynchronous operation. It doesn't subscribe to an event or pass a callback function to the target function. Instead, it allows the function to return a Promise, like this...

    Personal Mar 8, 2025
    1.1K00
  • Go Engineer System Course 018 [Study Notes]

    Getting Started with API Gateway and Continuous Deployment (Kong & Jenkins) corresponds to the course materials "Chapter 2: Getting Started with Jenkins" and "Chapter 3: Deploying Services with Jenkins", outlining the practical path for Kong and Jenkins in enterprise-level continuous delivery. Even with zero prior experience, you can follow the steps to build your own gateway + continuous deployment pipeline. Pre-class Introduction: What is an API Gateway? An API Gateway sits between clients and backend microservices...

    Personal Nov 25, 2025
    19000
  • In-depth Understanding of ES6 001 [Study Notes]

    Block-Level Scope Binding
    Previously, `var` variable declarations, regardless of where they were declared, were considered to be declared at the top of their scope. Since functions are first-class citizens, the typical order was `function functionName()`, followed by `var variable`.

    Block-Level Declarations
    Block-level declarations are used to declare variables that cannot be accessed outside the scope of a specified block. Block-level scope exists in:
    - Inside functions
    - Within blocks (the region between `{` and `}`)

    Temporal Dead Zone
    When the JavaScript engine scans code and finds variable declarations, it either hoists them to the top of the scope...

    Personal Mar 8, 2025
    1.6K00
  • Go Engineer System Course 010 [Study Notes]

    Install Elasticsearch (understand as a database) and Kibana (understand as a connection tool). The versions of ES and Kibana (port 5601) must be consistent.

    Learning Elasticsearch (ES) by comparison with MySQL: Terminology Mapping
    MySQL | Elasticsearch
    database | index (索引)
    table | type (fixed as _doc from 7.x, multiple types completely removed in 8.x...)

    Personal Nov 25, 2025
    32400
  • Go Engineering Systematic Course 014 [Study Notes]

    RocketMQ Quick Start. Go to our various configurations (podman) to see how it's installed. Introduction to Concepts: RocketMQ is a distributed messaging middleware open-sourced by Alibaba and an Apache top-level project. Core components: NameServer: Service discovery and routing; Broker: Message storage, delivery, and fetching; Producer: Message producer (sends messages); Consumer: Message consumer (subscribes to and consumes messages); Topic/Tag: Topic/...

    Personal Nov 25, 2025
    16700
EN
简体中文 繁體中文 English
欢迎🌹 Coding never stops, keep learning! 💡💻 光临🌹