protobuf的简单使用
操作系统 : CentOS7.3.1611_x64
gcc版本 :4.8.5
go 版本 : go1.8.3 linux/amd64
Python 版本 : 2.7.5
libprotoc : 2.5.0
Protobuf是Google开发一种数据描述语言,能够将结构化数据序列化,可用于数据存储、通信协议等方面。
首页: https://developers.google.com/protocol-buffers/
文档: https://developers.google.com/protocol-buffers/docs/overview
github地址: https://github.com/google/protobuf
这里记录了C++、Go和Python语言的简单使用示例,更多内容请参考官网手册。
准备工作
安装protobuf:
yum install protobuf protobuf-compiler protobuf-c-devel protobuf-devel
写proto文件(addr.book.proto):
package addr; message book { required int32 id = 1; required string str = 2; optional int32 opt = 3; }
C++示例
写文件操作(writer.cpp):
https://github.com/mike-zhang/cppExamples/blob/master/protobufOpt/example1/writer.cpp
读文件操作(reader.cpp):
https://github.com/mike-zhang/cppExamples/blob/master/protobufOpt/example1/reader.cpp
Makefile文件:
CC = g++ CFLAGS = -g -O2 -Wall SRC_DIR=. DST_DIR=libs all: make genproto make writer make reader genproto: mkdir -p $(DST_DIR) protoc -I=$(SRC_DIR) --cpp_out=$(DST_DIR) addr.book.proto writer: $(CC) -o writer writer.cpp $(DST_DIR)/addr.book.pb.cc -I$(DST_DIR) -lprotobuf reader: $(CC) -o reader reader.cpp $(DST_DIR)/addr.book.pb.cc -I$(DST_DIR) -lprotobuf clean: rm -rf $(DST_DIR) rm -f writer reader log rm -f *.o
运行效果:
[root@localhost proto1]# ./writer [root@localhost proto1]# ./reader 101 book1 [root@localhost proto1]#
Go示例代码
安装goprotobuf:
yum install golang-googlecode-goprotobuf.x86_64
将proto文件转换为go代码(genPbCode.sh):
#! /bin/sh mkdir -p src/addr protoc -I=. --go_out=src/addr addr.book.proto
修改protobuf路径:
vi src/addr/addr.book.pb.go import proto "code.google.com/p/goprotobuf/proto" 修改为: import proto "github.com/golang/protobuf/proto"
添加PATH(临时使用时可以这么操作):
export GOPATH=$GOPATH:$PWD
写文件操作(write.go):
https://github.com/mike-zhang/goExamples/blob/master/protobufOpt/protoTest1/write.go
读文件操作(reader.go):
https://github.com/mike-zhang/goExamples/blob/master/protobufOpt/protoTest1/read.go
运行效果:
[root@localhost proto3]# go run write.go [root@localhost proto3]# go run read.go id:11 str:"testMsg11" [root@localhost proto3]#
Python示例
需要安装protobuf包:
pip install protobuf
将proto文件转换为python代码(genPbCode.sh):
#! /bin/sh protoc -I=. --python_out=. addr.book.proto touch addr/__init__.py
写文件操作(write.py):
https://github.com/mike-zhang/pyExamples/blob/master/protobufOpt/example1/write.py
读文件操作(reader.py):
https://github.com/mike-zhang/pyExamples/blob/master/protobufOpt/example1/reader.py
运行效果:
(py27env) [root@localhost proto2]# ./genPbCode.sh (py27env) [root@localhost proto2]# python write.py (py27env) [root@localhost proto2]# python reader.py id: 1 str: "testMsg1" (py27env) [root@localhost proto2]#
好,就这些了,希望对你有帮助。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2018/20180201_protobuf的简单使用.rst
欢迎补充
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2012-02-01 获得CPU利用率(python调用top命令实现)