Using Google Protocol Buffer in Go/golang language

Source: Internet
Author: User

How to use Google protocol buffer in the go language?

Now the trend is a button to do, as with Ubuntu installation software

Go get code.google.com/p/goprotobuf/{proto,protoc-gen-go}

Go Install Code.google.com/p/goprotobuf/proto

Done, can be found under the $GO _path/bin protoc-gen-go This program, then can be practical protoc-gen-go to GO language proto file automatic generation.

go1.0 use: Protoc-gen-go--go_out=. Hellowrold.proto

go1.1 Direct Utility The following commands

Protoc--go_out=. Hellowrold.proto

Proto file:

Package LM;
Message HelloWorld
{
Required Int32 id = 1; Id
Required String str = 2; Str
Optional int32 opt = 3; Optional field

}

Package LM; Therefore must be placed in the LM directory (refer to the proto specification), use the command to generate files under LM

Protoc-gen-go--go_out=. Hellowrold.proto

Automatically survived the Helloworld.go file:

Code generated by Protoc-gen-go.
Source:helloworld.proto
Do not edit!

Package LM

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 Helloworld struct {
Id *int32 ' protobuf: "Varint,1,req,name=id" JSON: "Id,omitempty" '
Str *string ' protobuf: "Bytes,2,req,name=str" JSON: "Str,omitempty" '
Opt *int32 ' protobuf: "varint,3,opt,name=opt" JSON: "Opt,omitempty" '
xxx_unrecognized []byte ' JSON: "-" '
}

Func (this *helloworld) Reset () {*this = helloworld{}}
Func (this *helloworld) string () string {return proto.compacttextstring (this)}
Func (*helloworld) Protomessage () {}

Func (this *helloworld) GetId () int32 {
If this = Nil && this. Id! = Nil {
Return *this. Id
}
return 0
}

Func (this *helloworld) getstr () string {
If this = Nil && this. Str! = Nil {
Return *this. Str
}
Return ""
}

Func (this *helloworld) GetOpt () int32 {
If this = Nil && this. Opt! = Nil {
Return *this. Opt
}
return 0
}

Func init () {

}

You can see that there are no functions such as set_xx, Serializetoostream in C + + (you can see different methods from the code below).

Writer file:

Package Main

Import Proto "Code.google.com/p/goprotobuf/proto"
Import (
"./LM"
"FMT"
"OS"
)

Func Main () {

Msg: = &lm. helloworld{
Id:proto. Int32 (101),
Str:proto. String ("Hello"),
}//msg Init

Path: = String ("./log.txt")
F, err: = OS. Create (PATH)
If err! = Nil {
Fmt. Printf ("Failed:%s\n", err)
Return
}

Defer F.close ()
Buffer, err: = Proto. Marshal (msg)//serializetoostream
F.write (buffer)

}

Reader file:

Package Main

Import Proto "Code.google.com/p/goprotobuf/proto"
Import (
"./LM"
"FMT"
"IO"
"OS"
)

Func checkerror (err error) {
If err! = Nil {
Fmt. Println (Err. Error ())
Os. Exit (-1)
}
}

Func Main () {
Path: = String ("./log.txt")
File, err: = OS. Open (PATH)
If err! = Nil {
Fmt. Printf ("Failed:%s\n", err)
Return
}

Defer file. Close ()
FI, Err: = file. Stat ()
CheckError (ERR)
Buffer: = Make ([]byte, FI. Size ())
_, err = Io. Readfull (file, buffer)//read all content
CheckError (ERR)
Msg: = &lm. helloworld{}
Err = Proto. Unmarshal (buffer, msg)//unserialize
CheckError (ERR)
Fmt. Printf ("read:%s\n", Msg.) String ())

}

Original link: http://www.cnblogs.com/zhangqingping/

Using Google Protocol Buffer in Go/golang language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.