[TOC]

Fat Sir: don’t know if you have such doubt, found work, a lot of new friend GO contact installation protoc environment needed for a long time, just open when I installed the environment is also took a long time, finally how to also do not know the reason, that has installed online to find various resources, also does not have an article can be a good solution to this problem.

Soldier commander: this is easy to say, today let’s sort out and summarize, how to quickly and efficiently install the protoc environment

Preconditions, what does protoc do?

In short, Protoc is a tool for programming proto files into source files in various languages

For example, here we can proto file through protoc tools, compiled to generate suitable for GO language development source file XXX.pb. GO, generally will be used with GRPC, if interested in this, you can leave a message in the background oh, here first a single description of the protoc environment how to one-time deal

The protoc installation on Windows is relatively questionable at the moment, but it’s actually very simple to install, so let’s talk about it

Install go Protoc on Windows

1. Download the latest golang installer

Github.com/protocolbuf…

Unzip and add GOPATH’s path to the environment variable

For example, if you don’t know where to get GOPATH, you can type Go env on the terminal

3. Command line execution

go get github.com/golang/protobuf
Copy the code

If the preceding command fails to be executed, configure the proxy

Go env - w GO111MODULE = go on / / open the module env - w GOPROXY = https://goproxy.cn, direct / / enabling the agentCopy the code

4, % GOPATH%/src/github.com/golang/protobufxxxxx/protoc-gen-go

go build

go install
Copy the code

If the following program is displayed in the GOPATH/bin directory, the environment is successfully configured

5. Start using Protoc

The directory structure is:

test.proto

syntax="proto3"; / / version number
package mypro;  / / package name
enum ClassName{   / / the enumeration
    class1=0;  // Labels must start at 0
    class2=1;
    class3=2;
}
message Student{ // message corresponding to the structure of Go
  string name=1; //1: a unique identifier (equivalent to a database Id, not necessarily in the order of 1, 2).
  int32 age=2;  // The range of integers must be specified, such as int32, int64
  string address=3;
  ClassName cn=4;
}
message Students{
   repeated Student person=1;  // repeated modifier, equivalent to Go
   string school=2;
}
Copy the code

Protoc –go_out=. *.proto generates a.pb.go file

main.go

package main
import (
    "fmt"
    "github.com/golang/protobuf/proto"
    "mypro.com/mypro"  In go, all files in a directory should be set to one package
)
func main(a) {
    s1 := &mypro.Student{} // First student information
    s1.Name = "jz01"
    s1.Age = 23
    s1.Address = "cq"
    s1.Cn = mypro.ClassName_class2 // Enumeration type assignment
    
    ss := &mypro.Students{}
    ss.Person = append(ss.Person, s1) // Add the first student information to the corresponding section of Students
    
    s2 := &mypro.Student{}            // Second student information
    s2.Name = "jz02"
    s2.Age = 25
    s2.Address = "cd"
    s2.Cn = mypro.ClassName_class3
    ss.Person = append(ss.Person, s2) // Add the second student information to the corresponding section of Students
    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 serialized message is:", buffer)
    // Use UnmarshalMerge to preserve and append to existing data.
    data := &mypro.Students{}
    proto.Unmarshal(buffer, data)
    fmt.Println("Deserialized message is:", data)
}
Copy the code

In the same directory as main.go, run go mod init XXX, for example, go mod init mypro.com

Run the go build command in the main.go directory. If the go build command succeeds, the protobuf will be parsed correctly

When you need to use GRPC + protobuf, protoc tool compilation proto file, need to add GRPC plug-in, specific use way, interested can background private message Nezha Oh

The above is all the content of this issue. If you have any questions, you can put forward your questions in the comments section or backstage. We will communicate and grow together.

Good guy if the article is still useful to you, please help point a concern, share to your circle of friends, share technology, share happiness

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

Author: Nezha