分享
  1. 首页
  2. 文章

在 go/golang语言中使用 google Protocol Buffer

chuanheng · · 5989 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

怎么在go语言中实用google protocol Buffer呢?

现在的潮流趋势就是一键搞定,跟ubuntu安装软件一样

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

go install code.google.com/p/goprotobuf/proto

搞定,可以在 $GO_PATH/bin下找到 protoc-gen-go 这个程序,那么就可以实用protoc-gen-go 进行go语言的proto文件的自动生成了。

go1.0 使用: protoc-gen-go --go_out=. hellowrold.proto

go1.1 直接实用以下命令

protoc --go_out=. hellowrold.proto

proto文件:

package lm;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field

}

package lm; 因此必须放到lm目录下(参考proto规范) ,在lm下面使用命令生成文件

protoc-gen-go --go_out=. hellowrold.proto

自动生存了helloworld.go文件:

// 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 are 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() {

}

可以看到没有c++里面的set_xx、SerializeToOstream 之类的函数(可从下面的代码看到不同的方法)。

writer文件:

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文件:

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())

}

原文链接:http://www.cnblogs.com/zhangqingping/


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:chuanheng

查看原文:在 go/golang语言中使用 google Protocol Buffer

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
5989 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏