在 go/golang语言中使用 google Protocol Buffer
怎么在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/
在 go/golang语言中使用 google Protocol Buffer的更多相关文章
- 前端后台以及游戏中使用Google Protocol Buffer详解
前端后台以及游戏中使用Google Protocol Buffer详解 0.什么是protoBuf protoBuf是一种灵活高效的独立于语言平台的结构化数据表示方法,与XML相比,protoBuf更 ...
- Google Protocol Buffer 的使用和原理[转]
本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...
- Google Protocol Buffer的安装与.proto文件的定义
什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...
- Google Protocol Buffer 的使用和原理
Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...
- Google Protocol Buffer 协议
1. Protocol Buffers 简介 Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化 ...
- 【Google Protocol Buffer】Google Protocol Buffer
http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Google Protocol Buffer 的使用和原理 Protocol Buffers ...
- Google Protocol Buffer的安装与.proto文件的定义(转)
转自(https://www.cnblogs.com/yinheyi/p/6080244.html) 什么是protocol Buffer呢? Google Protocol Buffer( 简称 P ...
- 转Google Protocol Buffer 的使用和原理
Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...
- Google Protocol Buffer 的使用(一)
一.什么是Google Protocol Buffer下面是官网给的解释:Protocol buffers are a language-neutral, platform-neutral exten ...
随机推荐
- 大数据系列修炼-Scala课程11
接着昨天的list,也是学习集合的相关知识 ListBuffer.ArrayBuffer.Queue.stack相关操作 1.ListBuffer.ArrayBuffer代码实现:ListBuffer ...
- ACM之跳骚---ShinePans
Description Z城市居住着非常多仅仅跳蚤.在Z城市周六生活频道有一个娱乐节目.一仅仅跳蚤将被请上一个高空钢丝的正中央.钢丝非常长,能够看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有 ...
- Jquery zTree实例
zTree[简单介绍] zTree 是利用 JQuery 的核心代码,实现一套能完毕大部分经常使用功能的 Tree 插件 兼容 IE.FireFox.Chrome 等浏览器 在一个页面内可同一时候生成 ...
- hdu 3874
求一个序列中全部数字的和,当中数值同样的仅仅能计算一次. 先储存全部的请求,然后依照它们的右边界排序,在查询的同一时候更新区间.这里事实上有一点点DP的味道,在它进行某个查询之前,保证全部的反复数字( ...
- C--控制语句循环例子
C的三种循环while.for和do while 先说一下scanf()函数的返回值问题 当scanf("%d,%d",&a,&b);如果用户正确输入了俩个整形变 ...
- 动态类(Dynamic)应用
动态类(Dynamic)应用 背景: 在Coding中有时候会遇到一些需要解析的数据,可是数据的字段数量和名称未统一,我们没法定义实体类来对应.那么我们就会想到通过C#的dynamic动态类来实现,如 ...
- Java阅读word程序说明文件
完成office文件操作可以帮助apache.poi包(我用poi-3.10-FINAL),导入对应的jar包(最好所有导入) 以下的程序演示了一些操作word的过程,具体的函数功能能够查看此包的官方 ...
- C++多线程分析
我们开始谈论的线程之前先看看这些线载波前-进程. 进程,它是一个正在执行的程序的实例.是一个程序在其自身的地址空间的一次执行活动.进程是资源申请.调度.和独立执行的基本单位.进程有两部分组成: 1.操 ...
- PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings.
有三种解决办法: 1. php文件中设置时区 <?php date_default_timezone_set('Asia/Shanghai'); echo strtotime('2012-9-3 ...
- jmeter参数化之CSV Data Set Config
在jmeter中,可以用CSV Data Set Config实现参数化. 1.准备参数化数据