操作系统 : 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 = ;
required string str = ;
optional int32 opt = ;
}

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 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: 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:
str: "testMsg1" (py27env) [root@localhost proto2]#

好,就这些了,希望对你有帮助。

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2018/20180201_protobuf的简单使用.rst

欢迎补充

protobuf的简单使用的更多相关文章

  1. Protobuf的简单介绍、使用和分析

      Protobuf的简单介绍.使用和分析   一.protobuf是什么? protobuf(Google Protocol Buffers)是Google提供一个具有高效的协议数据交换格式工具库( ...

  2. Centos6.5下安装protobuf及简单使用

    1.protobuf是google公司提出的数据存储格式,详细介绍可以参考:https://code.google.com/p/protobuf/ 2.下载最新的protobuf,下载地址:https ...

  3. Centos6.4下安装protobuf及简单使用

    1.protobuf是google公司提出的数据存储格式,详细介绍可以参考:https://code.google.com/p/protobuf/ 2.下载最新的protobuf,下载地址:https ...

  4. .NET序列化工具Jil、Json.NET和Protobuf的简单测评

    前一段时间逛园子的时候发现有人比较了Jil.Json.NET和Protobuf的性能,一时好奇,也做了个测试,这里记录下来,以供查阅. 前期准备 依赖类库的话,可以通过Nuget在公共组件库总下载,这 ...

  5. Unity手游之路<一>C#版本Protobuf

    http://blog.csdn.net/janeky/article/details/17104877 个游戏包含了各种数据,包括本地数据和与服务端通信的数据.今天我们来谈谈如何存储数据,以及客户端 ...

  6. (转)RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)

    什么是ProtoBuf? 一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩 ...

  7. protobuf 中的嵌套消息的使用

    protobuf的简单的使用,不过还留下了一个问题,那就是之前主要介绍的都是对简单数据的赋值,简单数据直接采用set_xx()即可,但是如果不是简单变量而是自定义的复合类型变量,就没有简单的set函数 ...

  8. RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)

    什么是ProtoBuf? 一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩 ...

  9. SpringBoot整合Netty并使用Protobuf进行数据传输(附工程)

    前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会简单的介绍下用法,至于Netty在之前的文章中已经简单的介绍过了,这里就不 ...

随机推荐

  1. 空间数据可视化:1. 3D_Bar图表| 空间柱状图

    1.Sublime的使用 中文版的配置 https://jingyan.baidu.com/article/ca2d939d1e83feeb6c31cefc.html (百度经验) sublime里边 ...

  2. 054 kafka内部机制

    一:数据格式与数据存储 1.总结 存储在磁盘文件中(index+log) 顺序读写的 基于offset偏移量来管理数据的(主要是读操作) 由分区器根据key值决定数据分布到哪个分区,默认使用hash ...

  3. 使用 Django-debug-toolbar 优化Query 提高代码效率

    一段程序执行效率慢,除了cpu计算耗时外,还有一个很重要的原因是SQL的Duplicated过多,使用Django-debug-toolbar能够快速找出哪些地方的SQL可以优化,提高程序执行效率 1 ...

  4. 用面向对象重写thread 实现多次调用一个线程

    思路: 利用thread类中,run方法在子线程中调用,其他方法在主线程调用,所以将生产者写入主线程,将消费者写入run函数中在子线程中执行,完成生产者消费者模型 注意: 1. 要在 init 函数中 ...

  5. 解决Ubuntu中Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another proce...

    解决Ubuntu中Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another proce... ...

  6. Django分页(一)

    Django分页(一) 手动实现简单分页 HTML <!DOCTYPE html> <html lang="en"> <head> <me ...

  7. 从小白到区块链工程师:第一阶段:Go语言的HelloWorld初始(2)

    四.写下第一段Go语言代码“Hello World” 小建议:就是文件夹路径或者文件名称不要出现中文,可能会导致一些不必要的麻烦(编译失败) 在sublime中,我们在src文件夹上面,单击右键“Ne ...

  8. notepad++ 去空行

    Press Ctrl+H (Replace) Select Extended from SearchMode Put \r\n\r\n in Find What Put \r\n in Replace ...

  9. Alpha(1/10)

    鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...

  10. C# GridViewExportUtil

    using System.Data; using System.IO; using System.Web; using System.Web.UI; using System.Web.UI.WebCo ...