Go is proficient in protobuf serial one: install protobuf and protoc-gen-go

Time:2022-11-24

1. Introduction

protobuf is a language-neutral, platform-independent extensible plugin for serializing structured data.
You only need to define the file .proto of the protobuf structure, and then use the protoc command to generate a file with the structure of the corresponding programming language.

Protoc decouples protobuf from different languages ​​in design. The bottom layer uses C++ to realize the storage of protobuf structures, and then generates source codes in different languages ​​through plug-ins.

The compilation process of protoc can be divided into two simple steps

  1. Parse the .proto file, translate it into the native data structure of protobuf and save it in memory
  2. Pass the protobuf-related data structure to the compilation plug-in of the corresponding language, and the plug-in is responsible for rendering and outputting the language-specific template according to the received protobuf native structure.

Through this mechanism, protobuf supports most mainstream languages. Generated code such as Java, Python, Objective-C and C++ Kotlin, Dart, Go, Ruby and C#, etc.

2. Operating system installation protobuf

2.1 install protobuf

Here we use brew to install, just enter the following command

brew install protobuf

After the installation is complete, enter protoc –version to see if it takes effect

➜ protoc --version
libprotoc 3.21.7

Successful installation.

3. Install the go language tool protoc-gen-go corresponding to protobuf

3.1 Use go install to install the latest version of protoc-gen-go

go install github.com/golang/protobuf/[email protected]

Using go install will install the bin directory under $GOPATH
Let’s look at $GOPATH

➜ echo $GOPATH
/Users/hisheng/go/[email protected]

Then we enter this bin directory

➜ cd /Users/staff/go/[email protected]/bin

You will see the newly installed protoc-gen-go

Recommended Today

Wonderful review l Rust chat room: Xline cross-data center consistency management

On October 15, 2022, Datan Technology and the Rust language Chinese community cooperated in the Rust chat room event, hosted byShi Jicheng, co-founder of DatenLordI shared about open source distributed storage technology, focusing on Datan Technology’s new open source project Xline, how this cross-cloud metadata (metadata) KV storage product can achieve high-performance cross-data center data […]