This is a creation in Article, where the information may have evolved or changed.
If you do not understand Google Protocol buffer, you can first look at the Chinese information collected in this article: about Google Protocol buffer in Chinese
Source Address: https://code.google.com/p/goprotobuf/
Detailed Description: Https://code.google.com/p/goprotobuf/source/browse/README
Introduced:
This software have parts:a ' protocol compiler plugin ' that generates Go source files that, once compiled, can access a ND manage protocol buffers; and a library that implements Run-time support for encoding (marshaling), decoding (unmarshaling), and accessing protocol Buffers.
Effect:
The software consists of two parts: protoc-gen-go: Once the ' protocol compiler plugin ' is compiled, it generates some go source files that can access and manage protocol Buffers;proto: is a library that implements runtime support for encoding (marshalling), Decoding (de-compilation), and access Protocol buffers.
Simply put, Protoc-gen-go is a compiler that uses the *.proto protocol file to build *.pb.go files. In the next example, we'll show you how to use it.
Proto needs to be included in our software to encode the message as binary data or to restore the binary data to a message object.
SOURCE Download:
Go get-u code.google.com/p/goprotobuf/{proto,protoc-gen-go}
or download the source directly to your project:
HG Clone https://code.google.com/p/goprotobuf/
Such as:
Compile:
After compiling successfully, we will find that the Bin directory will generate the Protoc-gen-go file, and the corresponding library file will be generated under the PKG directory.
Next, we write an example of the test:
First, write the Test.proto, which reads as follows:
package example;
Enum FOO {X = 17;};
Message Test {
Required String label = 1;
Optional Int32 type = 2 [default=77];
Repeated int64 reps = 3;
Optional Group Optionalgroup = 4 {
Required String Requiredfield = 5;
}
}
Next, write the makefile file, which is used to generate the *.pb.go file with the following content:
Include:/code.google.com/p/goprotobuf/make.protobuf
All:regenerate
Regenerate:
Rm-f Test.pb.go
Make Test.pb.go
# The following rules is just aids to development. Not needed for typical testing.
Diff:regenerate
HG diff Test.pb.go
Restore
CP Test.pb.go.golden Test.pb.go
Preserve
CP Test.pb.go Test.pb.go.golden
Terminal, make, we will see the generated corresponding Test.pb.go file, the content is as follows:
Code generated by Protoc-gen-go.
Source:test.proto
Do not edit!
Package Example
Import Proto "Code.google.com/p/goprotobuf/proto"
Import JSON "Encoding/json"
Import Math "Math"
Reference Proto, JSON, and math imports to suppress error if they is not otherwise used.
var _ = Proto. Marshal
var _ = &json. syntaxerror{}
var _ = Math. Inf
Type FOO Int32
Const (
foo_x FOO = 17
)
var foo_name = map[int32]string{
: "X",
}
var foo_value = map[string]int32{
"X": 17,
}
Func (x FOO) Enum () *foo {
P: = new (FOO)
*p = X
Return P
}
Func (x FOO) string () string {
Return Proto. Enumname (Foo_name, Int32 (x))
}
Func (x FOO) Marshaljson () ([]byte, error) {
Return JSON. Marshal (x.string ())
}
Func (x *foo) Unmarshaljson (data []byte) error {
Value, err: = Proto. Unmarshaljsonenum (Foo_value, data, "FOO")
If err! = Nil {
return err
}
*x = FOO (value)
return Nil
}
Type Test struct {
Label *stringprotobuf:"bytes,1,req,name=label" json:"label,omitempty"
Type *int32protobuf:"varint,2,opt,name=type,def=77" json:"type,omitempty"
Reps []int64protobuf:"varint,3,rep,name=reps" json:"reps,omitempty"
Optionalgroup *test_optionalgroupprotobuf:"group,4,opt,name=OptionalGroup" json:"optionalgroup,omitempty"
xxx_unrecognized []bytejson:"-"
}
Func (this *test) Reset () {*this = test{}}
Func (this *test) string () string {return proto.compacttextstring (this)}
Func (*test) Protomessage () {}
Const Default_test_type Int32 = 77
Func (this *test) Getlabel () string {
If this = Nil && this. Label! = Nil {
Return *this. Label
}
Return ""
}
Func (this *test) GetType () int32 {
If this = Nil && this. Type! = Nil {
Return *this. Type
}
Return Default_test_type
}
Func (this *test) Getoptionalgroup () *test_optionalgroup {
If this = nil {
return this. Optionalgroup
}
return Nil
}
Type Test_optionalgroup struct {
Requiredfield *stringprotobuf:"bytes,5,req" json:"RequiredField,omitempty"
}
Func (this *test_optionalgroup) Reset () {*this = test_optionalgroup{}}
Func (this *test_optionalgroup) Getrequiredfield () string {
If this = Nil && this. Requiredfield! = Nil {
Return *this. Requiredfield
}
Return ""
}
Func init () {
Proto. Registerenum ("example. FOO ", Foo_name, Foo_value)
}
Finally, write the test code as follows:
Package Main
Import (
"Log"
"Code.google.com/p/goprotobuf/proto"
"Example"
"FMT"
)
Func Main () {
Test: = &example. test{
Label:proto. String ("Hello"),
Type:proto. Int32 (17),
Optionalgroup: &example. test_optionalgroup{
Requiredfield:proto. String ("Good Bye"),
},
}
Print test by string.
Fmt. PRINTLN (test. String ())
Data, err: = Proto. Marshal (Test)
If err! = Nil {
Log. Fatal ("Marshaling Error:", err)
}
Print data
Fmt. PRINTLN (data)
Newtest: = &example. test{}
Err = Proto. Unmarshal (data, newtest)
If err! = Nil {
Log. Fatal ("Unmarshaling error:", err)
}
Now test and newtest contain the same data.
if test. Getlabel ()! = Newtest.getlabel () {
Log. Fatalf ("Data mismatch%q! =%q", test. Getlabel (), Newtest.getlabel ())
}
etc.
}
Compiled and run with the following results:
Here is just a brief introduction and write a test example, many of the details are not introduced, the opportunity to share with you later.