This is a creation in Article, where the information may have evolved or changed.
Installing GOPROTOBUF
1. get protobuf compiler Protoc from https://github.com/google/protobuf/releases( binary version available for download to Windows
wget Https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gztar ZXVF PROTOBUF-2.6.1.TAR.GZCD Protobuf-2.6.1./configuremakemake installprotoc-h
2. get Goprotobuf Protobuf provided compiler plug-in Protoc-gen-go should be added path environment variable so that Protoc can find Protoc-gen-go
This plugin is Protoc used to compile a . proto file is a Golang source file that can be used by this source file to define the messages in the . Proto file.
Go get github.com/golang/protobuf/protoc-gen-gocd Github.com/golang/protobuf/protoc-gen-gogo buildgo installvi/etc/ Profile add $gopath/bin to the environment variable source profile
3. get The support library provided by GOPROTOBUF, including functions such as encoding (marshaling), decoding (unmarshaling), etc.
Go get github.com/golang/protobuf/protocd github.com/golang/protobuf/protogo buildgo Install
Using Goprotobuf
Here is an example to illustrate usage. First create a. proto file Test.proto:
Package Example;enum FOO {X = 17;}; Message Test {Required String label = 1;optional Int32 type = 2 [default=77];repeated Int64 reps = 3;optional Group Option Algroup = 4 {Required String requiredfield = 5;}}
Compile this. proto file:
Protoc--go_out=. *.proto
The protobuf compiler plugin protoc-gen-go provided by GOPROTOBUF is used here through –go_out. At this point we will generate a source file named Test.pb.go.
Before using it, let's look at some of the available interfaces for each PROTOBUF message in Golang:
-
Each PROTOBUF message corresponds to a golang struct
-
Message that the domain name Word is camel_case in the corresponding Golang structure for CamelCase
The setter method does not exist in the Golang struct that corresponds to the li>
message, only the struct can be assigned directly, and some auxiliary functions may be used when assigning values, for example:
msg. Foo = proto. A getter method exists in the Golang struct body of the String ("Hello")
-
Message that returns the value of the field and returns a default value if the field is not set to a value
-
Message non-repeate The field of D is implemented as a pointer to nil when the field is not set in the
-
Message repeated field is implemented as slice
-
When accessing enumeration values, use the Format enumeration type name _ Enum name (more content can Read the generated source directly)
-
uses Proto. Marshal is encoded using Proto. The Unmarshal function decodes the
Now we're writing a small program:
package mainimport ( "Log" // Auxiliary library "Github.com/golang/protobuf/proto" // test.pb.go path "Example") Func main () { // Create a message Test test := &example. test{ // setting the value of a field using an auxiliary function label: proto. String ("Hello"), type: proto. Int32 (+), optionalgroup: &example. Test_optionalgroup{ requiredfield: proto. String ("Good bye"), }, } // encode the data, err := proto. Marshal (tesT) if err != nil { Log. Fatal ("marshaling error: ", err) } // decode newtest := &example. Test{} err = proto. Unmarshal (data, newtest) if err != nil { log. Fatal ("unmarshaling error: ", err) } // test results if test. Getlabel () != newtest.getlabel () { log. Fatalf ("Data mismatch %q != %q", test. Getlabel (), newtest.getlabel ()) }}