Go to the official website first download PROTOBUF source code
Https://github.com/google/protobuf/releases
You can download the local and upload to the virtual machine first
I chose source code (tar.gz).
Install a dependent package (error may occur if a package is missing)
Yum install-y gcc gcc-c++ autoconf automake Libtool Curl Make g++ Unzip
After decompression, enter the protobuf-3.5.1 directory and run
./autogen.sh command
Execute the./configure command
Make && make install
Detect PROTOBUF is installed successfully?
Protoc-h
Because you want to use the go language, you need to install the appropriate plugin
First to the official website to download the plugin source code to Local:
https://github.com/golang/protobuf/releases
Uploading to a virtual machine for decompression
TAR-ZXVF protobuf-1.1.0.tar.gz
Renaming
MV Protobuf-1.1.0.tar.gz Protobuf
What is the value of querying goroot, as mine is:
Echo $GOROOT
/usr/local/gohome/go
Under the Goroot directory, create the following directory Src/github.com/golang
Mkdir-p /usr/local/gohome/go/src/github.com/golang
Move the Protobuf directory to the $goroot/src/github.com/golang directory
Enter $GOROOT/SRC/GITHUB.COM/GOLANG/PROTOBUF
Make install
Example:
Create a file manually first Test.proto
Specify version//note Proto3 and Proto2 are somewhat different syntax = "Proto3"; //package name, Protoc phone type when the go file is generated by package test;// /enum Type the first field must be a 0enum phonetype { home = 0; work = 1; } //Mobile message Phone { Phonetype type = 1; string number = 2; } //man message person { // The number that follows indicates the identification number int32 id = 1; string name = 2; //repeated means repeatable //can have multiple phones repeated phone phones = 3; } //Contact Book message contactbook { repeated person persons = 1; }
Execute the following command to generate the appropriate code file
Protoc--go_out=. *.proto
Or
Protoc--go_out=. Test.proto
In the go language, how to use it?
1. First create the test package, and then upload the generated test.pb.go to the test package
2. Writing test Cases
The contents are as follows:
Package Mainimport ("FMT" "Github.com/golang/protobuf/proto" "Io/ioutil" "OS" "xingej-go/xingej-go/xingej-go666/ Protobuf/test ") Func write () {p1: = &test. person{id:1, Name: "Spark",} book: = &test. contactbook{} book. Persons = Append (book. Persons, p1)//encoded data, _: = Proto. Marshal (book)//writes data to file Ioutil. WriteFile ("./pb_test.txt", data, OS.) Modeperm)}func Read () {data, _: = Ioutil. ReadFile ("./pb_test.txt") Book: = &test. contactbook{}//Decode data proto. Unmarshal (data, book) for _, V: = Range book. Persons {fmt. Println (V.id, V.name)}}func main () {write () read ()}
This article tests the sample and references the following article:
Https://www.cnblogs.com/jkko123/p/7161843.html
PROTOBUF installation and simple test cases based on the Go language