Golang里面使用protobuf(proto3)

Posted 拥剑公子的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang里面使用protobuf(proto3)相关的知识,希望对你有一定的参考价值。

原文:https://developers.google.com/protocol-buffers/docs/gotutorial

1.proto3文档:https://developers.google.com/protocol-buffers/docs/proto3
go api文档:https://godoc.org/github.com/golang/protobuf/proto

2.一个简单的proto文件,addressbook.proto,这个例子有如c语言的helloworld
https://github.com/google/protobuf/blob/master/examples/addressbook.proto
本文用的这个例子

3.执行指令:
go get -u github.com/golang/protobuf/protoc-gen-go
会下载protoc-gen-go.exe到$GOPATH/bin下,把它拷贝到$PATH,它只是一个插件,还不是真正的编译器

4.下载protoc,地址https://github.com/google/protobuf/releases,里面可以找到win/linux/mac的二进制文件,
我们需要protoc-3.1.0-win32.zip,把里面的protoc.exe(其它文件不需要)拷贝到PATH下面

5.生成c++和golang的代码
protoc --go_out . addressbook.proto
protoc --cpp_out . addressbook.proto
protoc --csharp_out . addressbook.proto
protoc --java_out . addressbook.proto
protoc --js_out . addressbook.proto
protoc --objc_out . addressbook.proto
protoc --ruby_out . addressbook.proto


6.测试
代码上传到github了(test pass):
https://github.com/wjx0912/GoLangUtils/tree/master/project_protobuf3

以上是关于Golang里面使用protobuf(proto3)的主要内容,如果未能解决你的问题,请参考以下文章

Protobuf 语言指南(proto3)

ProtoBuf练习

C++ProtoBuf的安装与使用

protobuf 学习 收藏的文章

golang 学习之grpc+ protobuf

使用 proto3 编译器编译 proto2 语法文件