在 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 ...
随机推荐
- 三个水杯 (bfs)
给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算.现在要求你写出一个程序,使其输出使初始状态到达目标状态的 ...
- Visual Studio 2010/2013 查看DLL接口(函数)
1. “应用程序" Visual Studio 2010/2013 的Visual Studio Tools文件夹中打开Visual Studio Command Prompt 命令提示窗口 ...
- 【spring教程之二】spring注射剂xml构造方法参数
1.上述续,假设你想注入bean当文件,传递给构造函数的参数.主要的变化是需要spring.xml配置文件来配置. <?xml version="1.0" encoding= ...
- CodeForces 425E Sereja and Sets
意甲冠军: 集S它包括了很多间隔[l,r] 和1<=l<=r<=n f(S)个不相交的区间 问给出n和f(S) 有几种可能的S集合 思路: dp好题 至于为啥是dp- 我 ...
- Skype发布视频API
原文:Skype发布视频API 相信很多人对Skype多少都应该有一些了解,如果以前没有使用过它的服务的话,也应该在最近的新闻中听说过它的大名.因为,它和我们每天都在接触的公司--Mircrosoft ...
- hdoj 1226 超级password 【隐图BFS】
称号:hdoj 1226 超级password 分析:这题属于隐式图搜索,状态不是非常明显,须要自己建立. 事实上搜索说白了就是暴力. 这个题目就是,首先对给出的能够组成的全部的数依次枚举.长度从小到 ...
- linux通过key区别登陆的人
key区分登录用户 脚本放 /etc/profile.d,会默认登录的时候执行, 类似于 #!/bin/bash # filename: /etc/profile.d/set_log_file.sh ...
- C#程序读取MAC地址的五种方法(转)
public class GetMac { ///<summary> /// 根据截取ipconfig /all命令的输出流获取网卡Mac ///</summary> ///& ...
- 基OOP知识
从今天开始,我开始总结GAO还通高老师<android道路的建筑师>,尝试一个星期写三个博客. 相对而言.看到这篇文章有点速度比你可以观看视频,刚才看的视频是更具体的.假设有兴趣,跟着我去 ...
- 使用SqlBulkCopy导入数据至MS SQL Server
原文:使用SqlBulkCopy导入数据至MS SQL Server Insus.NET一直使用表类型来数据入MS SQL Server.参考<存储过程参数为DataTable>http: ...