在 Golang 中使用 Protobuf
2016-01-06 11:17
573 查看
安装 goprotobuf
1.从 https://github.com/google/protobuf/releases 获取 Protobuf 编译器 protoc(可下载到 Windows 下的二进制版本
2.获取 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go(被放置于 $GOPATH/bin 下,$GOPATH/bin 应该被加入 PATH 环境变量,以便
protoc 能够找到 protoc-gen-go)
此插件被 protoc 使用,用于编译 .proto 文件为 Golang 源文件,通过此源文件可以使用定义在 .proto 文件中的消息。
3.获取 goprotobuf 提供的支持库,包含诸如编码(marshaling)、解码(unmarshaling)等功能
使用 goprotobuf
这里通过一个例子来说明用法。先创建一个 .proto 文件 test.proto:
编译此 .proto 文件:
这里通过 –go_out 来使用 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go。这时候我们会生成一个名为 test.pb.go 的源文件。
在使用之前,我们先了解一下每个 Protobuf 消息在 Golang 中有哪一些可用的接口:
每一个 Protobuf 消息对应一个 Golang 结构体
消息中域名字为 camel_case 在对应的 Golang 结构体中为 CamelCase
消息对应的 Golang 结构体中不存在 setter 方法,只需要直接对结构体赋值即可,赋值时可能使用到一些辅助函数,例如:
消息对应的 Golang 结构体中存在 getter 方法,用于返回域的值,如果域未设置值,则返回一个默认值
消息中非 repeated 的域都被实现为一个指针,指针为 nil 时表示域未设置
消息中 repeated 的域被实现为 slice
访问枚举值时,使用“枚举类型名_枚举名”的格式(更多内容可以直接阅读生成的源码)
使用 proto.Marshal 函数进行编码,使用 proto.Unmarshal 函数进行解码
现在我们编写一个小程序:
1.从 https://github.com/google/protobuf/releases 获取 Protobuf 编译器 protoc(可下载到 Windows 下的二进制版本
wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz tar zxvf protobuf-2.6.1.tar.gz cd protobuf-2.6.1 ./configure make make install protoc -h
2.获取 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go(被放置于 $GOPATH/bin 下,$GOPATH/bin 应该被加入 PATH 环境变量,以便
protoc 能够找到 protoc-gen-go)
此插件被 protoc 使用,用于编译 .proto 文件为 Golang 源文件,通过此源文件可以使用定义在 .proto 文件中的消息。
go get github.com/golang/protobuf/protoc-gen-go cd github.com/golang/protobuf/protoc-gen-go go build go install vi /etc/profile 将$GOPATH/bin 加入环境变量 source profile
3.获取 goprotobuf 提供的支持库,包含诸如编码(marshaling)、解码(unmarshaling)等功能
go get github.com/golang/protobuf/proto cd github.com/golang/protobuf/proto go build go install
使用 goprotobuf
这里通过一个例子来说明用法。先创建一个 .proto 文件 test.proto:
package example; enum FOO { X = 17; }; message Test { required string label = 1; optional int32 type = 2 [default=77]; repeated int64 reps = 3; optional group OptionalGroup = 4 { required string RequiredField = 5; } }
编译此 .proto 文件:
protoc --go_out=. *.proto
这里通过 –go_out 来使用 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go。这时候我们会生成一个名为 test.pb.go 的源文件。
在使用之前,我们先了解一下每个 Protobuf 消息在 Golang 中有哪一些可用的接口:
每一个 Protobuf 消息对应一个 Golang 结构体
消息中域名字为 camel_case 在对应的 Golang 结构体中为 CamelCase
消息对应的 Golang 结构体中不存在 setter 方法,只需要直接对结构体赋值即可,赋值时可能使用到一些辅助函数,例如:
msg.Foo = proto.String("hello")
消息对应的 Golang 结构体中存在 getter 方法,用于返回域的值,如果域未设置值,则返回一个默认值
消息中非 repeated 的域都被实现为一个指针,指针为 nil 时表示域未设置
消息中 repeated 的域被实现为 slice
访问枚举值时,使用“枚举类型名_枚举名”的格式(更多内容可以直接阅读生成的源码)
使用 proto.Marshal 函数进行编码,使用 proto.Unmarshal 函数进行解码
现在我们编写一个小程序:
package main import ( "log" // 辅助库 "github.com/golang/protobuf/proto" // test.pb.go 的路径 "example" ) func main() { // 创建一个消息 Test test := &example.Test{ // 使用辅助函数设置域的值 Label: proto.String("hello"), Type: proto.Int32(17), Optionalgroup: &example.Test_OptionalGroup{ RequiredField: proto.String("good bye"), }, } // 进行编码 data, err := proto.Marshal(test) if err != nil { log.Fatal("marshaling error: ", err) } // 进行解码 newTest := &example.Test{} err = proto.Unmarshal(data, newTest) if err != nil { log.Fatal("unmarshaling error: ", err) } // 测试结果 if test.GetLabel() != newTest.GetLabel() { log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel()) } }
相关文章推荐
- GTK解决Pango-WARNING **: Invalid UTF-8 string passed to pango_layout_set_text()问题
- [Machine Learning] Generative learning algorithm-GDA and NB
- Guava学习笔记:Google Guava 类库简介
- Django开发博客(五)——新增文章
- UML系列,使用UML实现GOF Design patterns,常用模式类图解读
- 最新Google V8 编译(2015-12-09) 之 HelloWorld
- POJ1003_Hangover
- mongo数据迁移三招
- Google Chrome 扩展程序开发
- Google微软IBM能打败亚马逊AWS的理由很丰满,进展却骨感
- Good Bye 2015B
- Running Spark GraphX algorithms on Library of Congress subject heading SKOS
- Good Bye 2015 A
- UVa 12019 - Doom's Day Algorithm
- Zoho Survey:给问卷加自己的logo
- django foreignkey外键使用 相当于left join
- EGOCache缓存框架详细讲解
- 蓝桥杯 寻找数组中最大值(sort排序+结构体)
- Django开发博客(四)——建立文章列表
- 用户登出Logout or Timeout