Golang版protobuf编译

官方网址:

https://developers.google.com/protocol-buffers/ (需要FQ)

代码仓库:

https://github.com/google/protobuf  (C++)

https://github.com/golang/protobuf  (Golang)

https://developers.google.com/protocol-buffers/docs/gotutorial (英文版教程)

 

本文以下部分按照英文版教程操作(windows  OS):

 

  1. 下载编译器,下载页面 https://github.com/google/protobuf/releases

最新发布版为V3.2.0,Url:https://github.com/google/protobuf/releases/download/v3.2.0/protoc-3.2.0-win32.zip

 

      2. 在cmd中运行如下命令,(前提:golang环境已经正确配置)

go get -u github.com/golang/protobuf/protoc-gen-go

下载完成在,在$GOPATH\src\github.com\golang\protobuf中可以查看源码

在$GOPATH\bin中可以找到生成的protoc-gen-go.exe,这是golang版的编译插件。

要保证$GOPATH\bin目录在环境变量$PATH中,这样protoc.exe就可以找到这个插件了。

 

通过以上两步,就完成了protobuf的golang编译环境准备。

下面将以源码包中的example代码为例进行编译。在https://github.com/google/protobuf/releases 页面下载V3.2.0版本的源码。

https://github.com/google/protobuf/archive/v3.2.0.zip

 

将压缩包解压到 D:\protobuf-3.2.0

D:\protobuf-3.2.0\examples 目录包含了golang示例代码。在该目录下创建名为tutorial的文件夹,将addressbook.proto移动到tutorial文件夹下,将前面下载的protoc.exe也拷贝到该目录下。

在命令行中,切换到D:\protobuf-3.2.0\examples\tutorial,执行命令

protoc --go_out=.\ addressbook.proto

生成 addressbook.pb.go 源文件,

修改 D:\protobuf-3.2.0\examples\add_person.go,将pb "github.com/google/protobuf/examples/tutorial"改为

pb "./tutorial"

 

命令行切换到D:\protobuf-3.2.0\examples目录执行,go build add_person.go

 在该目录下生成了 add_person.exe

 命令行执行 add_person.exe .\book.bin

按照提示,输入相关信息,录入完成后,数据被序列化到book.bin二进制文件中。

 

编译list_people.go,执行 list_people.exe .\book.bin命令可以查看反序列化之后的信息。

原文链接: https://www.cnblogs.com/majianguo/p/6492908.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Golang版protobuf编译

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/250115

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午4:16
下一篇 2023年2月14日 上午4:17

相关推荐