This is a creation in Article, where the information may have evolved or changed.
Installing the PROTOBUF Compilation tool
Download the latest version Https://github.com/golang/protobuf Official website Https://developers.google.com/protocol-buffers/docs/proto3 after decompression execution
./autogen.sh If you encounter a program that is not supported, install it./configuremakemake Installation
installing the Golang Support library
Download Https://github.com/golang/protobuf Building the catalog in the project SRC directory
github.com/golang/protobuf/
Copy all downloaded protobuf to this directory CD to this directory to execute
Make
The Protoc-gen-go executable is compiled, which is provided to the PROTOBUF compilation tool using the
Test
Create a folder in the code directory where proto files are placed, such as protocfg create. proto file configuration PROTOBUF data structures such as
Syntax = "Proto3";p ackage prototest;enum FOO {X = 0;}; Message Test {string label = 1;int32 type = 2; int64 reps = 19;}
Execute in this directory
/usr/local/bin/protoc--plugin={}bin/protoc-gen-go--go_out=. Test.proto
will be generated test.bp.go This file is used in the GO program:
Package Mainimport ("Demo/prototest" "FMT" "Libs/plog" "Github.com/golang/protobuf/proto") func Test () {fmt. PRINTLN ("Begin Testlog ...")//proto. Bool (FALSE) Test: = &prototest. Test{label: "Hello", type:17, Reps:34}data, err: = Proto. Marshal (test) if err! = Nil {plog. Println (Plog. Warning, "Demo", Err. Error ())} else {untest: = &prototest. Test{}err: = Proto. Unmarshal (data, untest) if Err = = Nil {fmt. Println ("Undata", Untest.label, Untest.reps, Untest.type)}}fmt. PRINTLN ("Data", data)}func main () {Test ()}