在 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 ...
随机推荐
- .net Framework各个版本之间的发展
原文:.net Framework各个版本之间的发展 上个星期看到了.NET 4.0框架退休日期逐渐临近文章,发现自己一直在使用NET FrameWork,身为一个NET程序员,里面大概的区别自己还 ...
- hdu Tempter of the Bone (奇偶剪枝)
学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...
- 未能加载文件或程序集 Newtonsoft.Json, Version=4.5.0.0 的报错,解决方法
使用httpclient测试webapi的时候客户端报错: {"未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, P ...
- jquery.validate1.13
jquery.validate新的写法(jquery.validate1.13.js) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- 【Leetcode】Sort List (Sorting)
这个问题需要与归并排序排两个名单,基本思路分为切割与合并 合并后的代码Merge Two Sorted List里已经讲得非常清楚了. 所以这里直接给出代码. public ListNode merg ...
- Ruby入门--Linux/Windows下的安装、代码开发及Rails实战
Ruby入门--Linux/Windows下的安装.代码开发及Rails实战 http://www.linuxidc.com/Linux/2014-04/100242.htm Ubuntu 13.04 ...
- Appium键盘操作
方法1 AppiumDriver实现了在上述功能,代码如下(java版本) driver.sendKeyEvent(66);方法2 HashMap<String, Integer> key ...
- 速度 Github
首先需要了解.git 是版本号的管理工具,为了能够把任意代码托管执照:github 其中一个是. 应用 github 什么不该说的帐户. 那么,申请后,在需求 github 并建立了独特的本地机器上的 ...
- FPGA笔记-阅读.dat文件
阅读.dat图像文件 .dat文件是matlab生成的图像文件 initial begin // Initialize Inputs CLK = 0; RST = 1; IMAGE_DATA = 0; ...
- vs2005中的WebBrowser控件的简单应用
原文:vs2005中的WebBrowser控件的简单应用 这个控件被封装了一下,和以前的调用方式稍有不同.事件还是那几个,变化不大.方法变了不少.从网上能查到的资料不多,贴出一些代码来作参考.看看这段 ...