[TOC]
I don’t know if you have any doubts about this, but I found that many new users need a long time to install the Protoc environment
At the beginning, it took me a long time to install the environment. I do not know why it was installed in the end. I searched all kinds of resources on the Internet, but none of the articles could solve this problem well.
Private Commander: That’s all right. Today, let’s review and summarize. How to install the PROTOC environment quickly and efficiently
So what does Protoc do?
In short, Protoc is a tool for programming proto files into source files in a variety of languages
For example, here we can proto file through the PROTOC tool, compiled and generated source code file suitable for GO language development XXX. PB. GO, generally used with GRPC, if interested in this, you can leave a message in the background oh, here and first a single PROTOC environment how to do it at one time
So far Protoc installation on Windows is relatively problematic, but it’s actually very simple to install, so let’s talk about it
Install Go Protoc on Windows
1. Download the latest version of Golang installer
https://github.com/protocolbuffers/protobuf/releases/
2. Unzip and add the path of GOPATH to the environment variable
For example, if you don’t know where to get GOPath, you can type in Go env in your terminal to check it out
3. Command line execution
go get github.com/golang/protobuf
If the above command fails, please configure the agent
Go env - w GO111MODULE = go on / / open the module env - w GOPROXY = https://goproxy.cn, direct / / enabling the agent
4, % GOPATH%/src/github.com/golang/protobufxxxxx/protoc-gen-go
go build
go install
After execution, you will see the following program generated in your goPath /bin directory, indicating that the environment has been successfully configured
5. Start using Protoc
The directory structure is:
test.proto
syntax="proto3"; // Package MyPro; Class1 =0; // Enum ClassName{// Enumerate ClassName =0; // Label must start at 0 class2=1; class3=2; } message Student{// Message, corresponding to the Go structure String name=1; / / 1: label, only can (equivalent to Id in the database, not necessarily from 1, 2 in the order.) int32 age=2; Int address=3; int address=3; int address=3; ClassName cn=4; } message Students{ repeated Student person=1; String school=2 (String school=2); }
Executing: protoc –go_out=. *. Proto will generate the.pb.go file
main.go
Package the main import (" FMT "introduced" github.com/golang/protobuf/proto "mypro.com/mypro" / / "proto bag, go, Func main() {s1 := &myPro.student {} // First Student info s1.name = "jz01" s1.age = 23 s1.address = "cq" S1.cn = mypro.ClassName_class2 // Enumeration ss := &mypro.Students{} ss.Person = append(ss.Person, ss.Person, ss.Person, ss.Person, ss. S2. Name = "jz02" s2.Age = 25 s2.Address = "CD" s2.cn = "s2.cn" s2.cn = "s2.cn" s2.cn = "jz02" s2.age = 25 "s2.address =" CD "s2.cn = mypro.ClassName_class3 ss.Person = append(ss.Person, Ss.School = "cqu" FMT.Println("Students' information is: ", ss) // Marshal takes a protocol buffer message // and encodes it into the wire format, returning the data. buffer, _ := proto.Marshal(ss) fmt.println (" The message after serialization is: ", buffer) // Use UnmarshalMerge to preserve and append to existing data. data := &mypro.Students{} proto.Unmarshal(buffer, Data) fmt.println (" information after deserialization is: ", data)}
Go mod init XXX in the same directory as main.go, modularized as: go mod init mypro.com
Go in a directory at the same level as main.go and, if successful, resolve ProtoBuf correctly
When you need to use GRPC + Protobuf, you need to add GRPC plug-in when compiling proto files with the Protoc tool. If you are interested in the specific way of using it, you can send a background text to Nezha
That’s all for this episode. If you have any questions, please feel free to post them in the comments section or back stage. Let’s communicate and grow together.
Boy, if this article still has some effect on you, please help to point attention, share your circle of friends, share technology, share happiness
Technology is open, our mentality, should be open. Embrace change, live up to the sun, and strive to move forward.
Author: Nezha