之前的文章中已经展示过如何在C++中使用protobuf,本文将简单示范protobuf在GolangPython中的使用。

Talk is cheap. Show you my code.

首先是Python:

import addressbook_pb2
import random
import uuid def Home():
return addressbook_pb2.Person.HOME def Work():
return addressbook_pb2.Person.WORK def Mobile():
return addressbook_pb2.Person.MOBILE switch = {0: Mobile(), 1: Home(), 2: Work()} # from protobuf to string def Write2AB(addressbook):
for i in range(20):
person = addressbook.people.add()
person.id = i
person.name = 'name: ' + str(i)
person.email = str(i) + '@eamil.com' phone = person.phones.add()
phone.type = switch[random.randint(0, 999) % 3]
phone.number = str(uuid.uuid1()) return addressbook.SerializeToString() def ReadFromAB(addressbook):
i = 1
for people in addressbook.people:
print('\n====== {} ======'.format(i))
print('Person ID: ', people.id)
print('Person Name: ', people.name)
print('Person Email: ', people.email) print('Person PhoneNumber:')
for phone in people.phones:
if phone.type == addressbook_pb2.Person.MOBILE:
print('\t type:Mobile number:{}'.format(phone.number))
elif phone.type == addressbook_pb2.Person.HOME:
print('\t type:Home number:{}'.format(phone.number))
if phone.type == addressbook_pb2.Person.WORK:
print('\t type:work number:{}'.format(phone.number))
i += 1 if __name__ == "__main__":
ab = addressbook_pb2.AddressBook()
abString = Write2AB(ab)
print(abString) # from string to protobuf
ab.ParseFromString(abString)
ReadFromAB(ab)

接下来是Golang:

package main

import (
"fmt"
"strconv" uuid "github.com/satori/go.uuid" "github.com/golang/protobuf/proto"
"github.com/zj/tutorial"
) // Write2AB return []byte from protobuf
func Write2AB(ab *tutorial.AddressBook) ([]byte, error) {
for i := 0; i < 10; i++ {
person := &tutorial.Person{}
person.Id = int32(i)
person.Name = "name" + strconv.Itoa(i)
person.Email = strconv.Itoa(i) + "@email.com" phone := &tutorial.Person_PhoneNumber{}
switch i % 3 {
case 0:
phone.Type = tutorial.Person_MOBILE
case 2:
phone.Type = tutorial.Person_WORK
default:
phone.Type = tutorial.Person_HOME
}
phone.Number = uuid.Must(uuid.NewV4()).String() person.Phones = append(person.Phones, phone) ab.People = append(ab.People, person)
} return proto.Marshal(ab) } // ReadFromBytes read addressbook from []byte
func ReadFromBytes(abByte []byte) (*tutorial.AddressBook, error) {
ab := &tutorial.AddressBook{}
err := proto.Unmarshal(abByte, ab)
if err != nil {
return nil, err
}
return ab, nil
} func main() {
ab := &tutorial.AddressBook{} abBytes, err := Write2AB(ab)
if err != nil {
fmt.Println(err)
return
} ab, err = ReadFromBytes(abBytes)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ab)
}

本文采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。

Protobuf示例:Golang and Python的更多相关文章

  1. 2017年的golang、python、php、c++、c、java、Nodejs性能对比(golang python php c++ java Nodejs Performance)

    2017年的golang.python.php.c++.c.java.Nodejs性能对比 本人在PHP/C++/Go/Py时,突发奇想,想把最近主流的编程语言性能作个简单的比较, 至于怎么比,还是不 ...

  2. 2017年的golang、python、php、c++、c、java、Nodejs性能对比[续]

    2017年的golang.python.php.c++.c.java.Nodejs性能对比[续] 最近忙,这个话题放了几天,今天来个续集.   上篇传送门: 2017年的golang.python.p ...

  3. golang与python多线程的并发速度

    一.golang的代码 package main import ( "fmt" "time" ) func Text_goroute(a int, b int) ...

  4. 2018-06-21 中文代码示例视频演示Python入门教程第五章 数据结构

    知乎原链 续前作: 中文代码示例视频演示Python入门教程第四章 控制流 对应在线文档: 5. Data Structures 这一章起初还是采取了尽量与原例程相近的汉化方式, 但有些语义较偏(如T ...

  5. 2018-06-20 中文代码示例视频演示Python入门教程第四章 控制流

    知乎原链 续前作: 中文代码示例视频演示Python入门教程第三章 简介Python 对应在线文档: 4. More Control Flow Tools 录制中出了不少岔子. 另外, 输入法确实是一 ...

  6. 想涨工资吗?那就学习Scala,Golang或Python吧

    [编者按]据薪水调查机构 PayScale 提供的数据显示,掌握 Scala,Golang 和 Python 语言以及诸如 Apache Spark 之类的大数据技术,能带来最大的薪水提升.本文作者为 ...

  7. golang和python互相调用

    http://blog.yuanzhaoyi.cn/2018/06/27/golang_python.html python3-ctypes: https://docs.python.org/3.5/ ...

  8. Golang调用Python

    https://yq.aliyun.com/articles/117329 Python是时髦的机器学习御用开发语言,Golang是大红大紫的新时代后端开发语言.Python很适合让搞算法的写写模型, ...

  9. DES对称加密算法实现:Java,C#,Golang,Python

    数据加密标准(DES,Data Encryption Standard,简写DES)是一种采用块密码加密的对称密钥算法,加密分组长度为64位,其中8.16.24.32.40.48.56.64 等8位是 ...

  10. 2018-06-20 中文代码示例视频演示Python入门教程第三章 简介Python

    知乎原链 Python 3.6.5官方入门教程中示例代码汉化后演示 对应在线文档: 3. An Informal Introduction to Python 不知如何合集, 请指教. 中文代码示例P ...

随机推荐

  1. Java 获取当前天是一年中的第几天

    Java 获取当前天是一年中的第几天 @Test void dayofweed() throws Exception { System.out.println("2023-01-01 第 & ...

  2. Runnable接口的 run() 方法和start()方法

    1.start()方法来启动线程,真正实现了多线程运行.这时无需等待run方法体代码执行完毕,可以直接继续执行下面的代码:通过调用Thread类的start()方法来启动一个线程, 这时此线程是处于就 ...

  3. Docker 安装 ELK,EFK代替

    ELK 版本因为 前面 Elasticsearch 用的 7.9.3 版本,所以 kibana-7.9.3.logstash-7.9.3 都用 7.9.3 版本 安装配置 Elasticsearch ...

  4. 阿里云视频云vPaaS低代码音视频工厂:极速智造,万象空间

    当下音视频技术越来越广泛地应用于更多行各业中,但因开发成本高.难度系数大等问题,掣肘了很多企业业务的第二增长需求.阿里云视频云基于云原生.音视频.人工智能等先进技术,提供易接入.强拓展.高效部署和覆盖 ...

  5. stm32f103 rt-thread fal easyflash移植过程

    需求:使用Stm32F103片内Flash进行数据存储 硬件平台:Stm32F103C8t6(由于Stm32F103C8t6的Flash标注为64k实际为128K,64KFlash在使用rt-thre ...

  6. Python日常学习

    学习算法和数据结构之余开始接触python,目前正在学习语法部分,这篇blog记录下一些知识点和放一些文档 or 教程的传送门. 文档网站 Python 解释器内置函数 Python速成 from O ...

  7. Oracle JDK7 bug 发现、分析与解决实战

    本文首发于 vivo互联网技术 微信公众号 链接: https://mp.weixin.qq.com/s/8f34CaTp--Wz5pTHKA0Xeg作者:vivo 官网商城开发团队 众所周知,Ora ...

  8. 每天学五分钟 Liunx 110 | 存储篇:RAID

    RAID RAID 是廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks)的意思.通过它可以将较小的磁盘组成较大的磁盘.   RAID 模式 RAID 有几种模 ...

  9. Linux系统CPU异常占用(minerd 、tplink等挖矿进程)

    转载请注明出处: 云服务器ECS(Linux) CPU使用率超过70%,严重时可达到100%,服务器响应越来越慢.  服务器中存在恶意minerd.tplink进程 该进程是服务器被入侵后,被恶意安装 ...

  10. Postman 接口测试配置 Pre-request Script

    本文为博主原创,转载请注明出处:  Pre-request Script 为Postman预置脚本,用于在postman 发送请求之前执行,封装计算或获取某些请求参数. 1. postman 脚本提供 ...