Google Go Programming Language that was released last November has really impressed me and others worldwide if u've been monitoring it.
But the inclusion of the Protocol Buffer Library is a boost for people like me who prefers serializing to other technologies like XML. I believe I will have cleaner syntax with less lines of code compared to using XML.
For those interested in using Protocol Buffers with Go Lang, visit
this link or if you prefer using it with other languages like Java, Python and C++, visit the project homepage
here.
The simplest way to describe this is to see an example. Given file test.proto, containing
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;
};
}
The resulting file, test.pb.go, is:
package example
import "goprotobuf.googlecode.com/hg/proto"
type FOO int32
const (
FOO_X = 17
)
var FOO_name = map[int32] string {
17: "X",
}
var FOO_value = map[string] int32 {
"X": 17,
}
func NewFOO(x int32) *FOO {
e := FOO(x)
return &e
}
type Test struct {
Label *string "PB(bytes,1,req,name=label)"
Type *int32 "PB(varint,2,opt,name=type,def=77)"
Reps []int64 "PB(varint,3,rep,name=reps)"
Optionalgroup *Test_OptionalGroup "PB(group,4,opt,name=optionalgroup)"
XXX_unrecognized []byte
}
func (this *Test) Reset() {
*this = Test{}
}
func NewTest() *Test {
return new(Test)
}
const Default_Test_Type int32 = 77
type Test_OptionalGroup struct {
RequiredField *string "PB(bytes,5,req)"
XXX_unrecognized []byte
}
func (this *Test_OptionalGroup) Reset() {
*this = Test_OptionalGroup{}
}
func NewTest_OptionalGroup() *Test_OptionalGroup {
return new(Test_OptionalGroup)
}
func init() {
proto.RegisterEnum("example.FOO", FOO_name, FOO_value)
}
To create and play with a Test object:
package main
import (
"log"
"goprotobuf.googlecode.com/hg/proto"
"./example.pb"
)
func main() {
test := &example.Test {
Label: proto.String("hello"),
Type: proto.Int32(17),
Optionalgroup: &example.Test_OptionalGroup {
RequiredField: proto.String("good bye"),
},
}
data, err := proto.Marshal(test)
if err != nil {
log.Exit("marshaling error:", err)
}
newTest := example.NewTest()
err = proto.Unmarshal(data, newTest)
if err != nil {
log.Exit("unmarshaling error:", err)
}
// Now test and newTest contain the same data.
if proto.GetString(test.Label) != proto.GetString(newTest.Label) {
log.Exit("data mismatch %q %q", proto.GetString(test.Label), proto.GetString(newTest.Label))
}
// etc.
}