Go使用protobuf
WIN7 + Go1.9.2+protobuf3.5.1
1.首先定义一个用于测试的proto文件test.proto,内容如下:
syntax = "proto3";
package example; message Test {
string strTest = ;
double dTest = ;
repeated int64 i64RepsTest = ;
}
2.需要下载两个exe来生成对应的go文件
①https://github.com/google/protobuf/releases下载protoc-3.5.1-win32.zip文件,解压得到protoc.exe
②使用go get github.com/golang/protobuf/protoc-gen-go
下载生成go格式代码的插件(默认会下载到GOPATH/bin中,而GOPATH/bin默认是c:/Users/Administrator/go/bin),得到protoc-gen-go.exe
③将protoc.exe、protoc-gen-go.exe和test.proto拷贝到同一个文件夹中
④protoc.exe --go_out=. test.proto,生成对应的go代码文件test.pb.go
3.下面使用代码
①使用go get github.com/golang/protobuf/proto
安装protobuf库
②将生成的test.pb.go拷贝到项目中(这里必须注意,因为使用了package example,所以必须在项目中新建example文件夹再拷贝进去)
③在项目中使用protobuf库,以及引入Test
import (
"github.com/golang/protobuf/proto"
"../example" //这里根据项目结构决定example的位置
)
④测试代码(在这里测试了中文字符串,double数据以及int64的数据)
marshalTest := &example.Test{
*proto.String("test_string中文test"),
*proto.Float64(2.34),
[]int64{, , }} data, err := proto.Marshal(marshalTest)
if err != nil {
fmt.Println("proto.Marshal err : ", err)
} unmarshalTest := &example.Test{}
err = proto.Unmarshal(data, unmarshalTest) if err != nil {
fmt.Println("proto.Unmarshal err : ", err)
} fmt.Println("strTest = ", unmarshalTest.GetStrTest())
fmt.Println("doubleTest = ", unmarshalTest.GetDTest())
for _, v := range unmarshalTest.GetI64RepsTest() {
fmt.Println("int64 reps = ", v)
}
输出的结果
以上。
参考博文:《golang使用protobuf》
PS:protobuf相关
Go使用protobuf的更多相关文章
- python通过protobuf实现rpc
由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行g ...
- Protobuf使用规范分享
一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...
- java netty socket库和自定义C#socket库利用protobuf进行通信完整实例
之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...
- 在Wcf中应用ProtoBuf替代默认的序列化器
Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...
- protobuf的编译安装
github地址:https://github.com/google/protobuf支持多种语言,有多个语言的版本,本文采用的是在centos7下编译源码进行安装. github上有详细的安装说明: ...
- 编译protobuf的jar文件
1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...
- protobuf学习(2)-相关学习资料
protobuf官方git地址 protobuf官方英文文档 (你懂的需要FQ) protobuf中文翻译文档 protobuf概述 (官方翻译 推荐阅读) protobuf入门 ...
- google protobuf安装与使用
google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...
- c# (ENUM)枚举组合类型的谷歌序列化Protobuf
c# (ENUM)枚举组合类型的谷歌序列化Protobuf,必须在序列化/反序列化时加上下面: RuntimeTypeModel.Default[typeof(Alarm)].EnumPassthru ...
- dubbox 增加google-gprc/protobuf支持
好久没写东西了,今年实在太忙,基本都在搞业务开发,晚上来补一篇,作为今年的收官博客.google-rpc 正式发布以来,受到了不少人的关注,这么知名的rpc框架,不集成到dubbox中有点说不过去. ...
随机推荐
- H5+JS生成验证码
效果图如下: <canvas id="canvas1" style="margin-left: 200px;"></canvas>< ...
- FileReader 获取图片base64数据流 并 生成图片
<?php if(isset($_GET['upload']) && $_GET['upload'] == 'img'){ if(isset($_GET['stream_type ...
- Fiddler(一)Fiddler介绍及应用场景
Fiddler是一款网络抓包工具,抓包可以是抓取电脑端请求的数据,还可以抓取移动端(手机APP)的数据包,可以监控HTTP和HTTPS的流量,可以通过浏览器或者客户端软件向服务器发送的HTTP或者HT ...
- Bugku-CTF之flag.php(点了login咋没反应)
Day2 flag.php(点了login咋没反应) 地址:http://123.206.87.240:8002/flagphp/
- 如何利用好github的问题
github对我来说真的是一个超好的平台,不过之前只是把它仓库来使用, 后来在大佬告诉我应该怎么使用github,今天就来总结下如何利用好github,让它发挥最大的威力. 1.把github当做百科 ...
- python --- 03 整型 bool 字符串 for循环
一.整型(int) 基本操作: 1.+ - * / % // ** 2. .bit_length() 计算整数在内存中占⽤的⼆进制码的⻓度 如: 二.布尔值(bool) True False 1. ...
- 本地git远程github
操作环境: windows系统 本地已安装git 步骤 1.右键进入git bash 2.进入本地仓库,如果有,则跳到步骤3.如果没有,则找到要作为仓库的文件夹,例如e://git//reposito ...
- Install Apache Maven on Ubuntu
Download the Apache maven from this link https://maven.apache.org/download.cgi, extract the download ...
- Python3 tkinter基础 Entry state 不可写 可以选 可复制的输入框
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- FancyBox的使用技巧 (汇总)
http://note.youdao.com/share/?id=1c8373249f523529a6b6dcde60777400&type=note#/