在 Golang 中使用 Protobuf
- wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz
tar zxvf protobuf-2.6.1.tar.gz- cd protobuf-2.6.1
./configure- make
- make install
- protoc -h
- go get github.com/golang/protobuf/protoc-gen-go
- cd github.com/golang/protobuf/protoc-gen-go
- go build
- go install
- vi /etc/profile 将$GOPATH/bin 加入环境变量
- source profile
- go get github.com/golang/protobuf/proto
- cd github.com/golang/protobuf/proto
- go build
- go install
使用 goprotobuf
这里通过一个例子来说明用法。先创建一个 .proto 文件 test.proto:
- package example;
- enum FOO { X = 17; };
- message Test {
- required string label = 1;
- optional int32 type = 2 [default=77];
- repeated int64 reps = 3;
- optional group OptionalGroup = 4 {
- required string RequiredField = 5;
- }
- }
编译此 .proto 文件:
- protoc --go_out=. *.proto
这里通过 –go_out 来使用 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go。这时候我们会生成一个名为 test.pb.go 的源文件。
在使用之前,我们先了解一下每个 Protobuf 消息在 Golang 中有哪一些可用的接口:
每一个 Protobuf 消息对应一个 Golang 结构体
消息中域名字为 camel_case 在对应的 Golang 结构体中为 CamelCase
消息对应的 Golang 结构体中不存在 setter 方法,只需要直接对结构体赋值即可,赋值时可能使用到一些辅助函数,例如:
- msg.Foo = proto.String("hello")
消息对应的 Golang 结构体中存在 getter 方法,用于返回域的值,如果域未设置值,则返回一个默认值
消息中非 repeated 的域都被实现为一个指针,指针为 nil 时表示域未设置
消息中 repeated 的域被实现为 slice
访问枚举值时,使用“枚举类型名_枚举名”的格式(更多内容可以直接阅读生成的源码)
使用 proto.Marshal 函数进行编码,使用 proto.Unmarshal 函数进行解码
现在我们编写一个小程序:
- package main
- import ( "log"
- // 辅助库
- "github.com/golang/protobuf/proto"
- // test.pb.go 的路径
- "example")
- func main() { // 创建一个消息 Test
- test := &example.Test{ // 使用辅助函数设置域的值
- Label: proto.String("hello"),
- Type: proto.Int32(17),
- Optionalgroup: &example.Test_OptionalGroup{
- RequiredField: proto.String("good bye"),
- },
- } // 进行编码
- data, err := proto.Marshal(test)
if err != nil {- log.Fatal("marshaling error: ", err)
- } // 进行解码
- newTest := &example.Test{}
- err = proto.Unmarshal(data, newTest)
if err != nil {- log.Fatal("unmarshaling error: ", err)
- } // 测试结果
- if test.GetLabel() != newTest.GetLabel() {
- log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
- }
- }
在 Golang 中使用 Protobuf的更多相关文章
- google的grpc在golang中的使用
GRPC是google开源的一个高性能.跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,基于Netty 4.x. 前面写过一篇golang标准库的rpc包的用法,这篇文章接着讲一 ...
- python golang中grpc 使用示例代码详解
python 1.使用前准备,安装这三个库 pip install grpcio pip install protobuf pip install grpcio_tools 2.建立一个proto文件 ...
- 【新手笔记】golang中使用protocol buffers 3
主要参考了这篇帖子:https://segmentfault.com/a/1190000009277748 1.下载windows版本的PB https://github.com/protocolbu ...
- golang中的rpc开发
golang中实现RPC非常简单,官方提供了封装好的库,还有一些第三方的库 golang官方的net/rpc库使用encoding/gob进行编解码,支持tcp和http数据传输方式,由于其他语言不支 ...
- 在Wcf中应用ProtoBuf替代默认的序列化器
Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...
- webapi 中使用 protobuf
相比json来说,好处是速度更快,带宽占用更小.其效果大致等于json+Gzip. 在webapi中使用protobuf的方法为: 引用nuget包 Install-Package protobuf- ...
- golang中的race检测
golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...
- 基础知识 - Golang 中的正则表达式
------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...
- 在网络通讯中应用Protobuf
在网络通讯中应用Protobuf Protobuf的设计非常适用于在网络通讯中的数据载体,它序列化出来的数据量少再加上以K-V的方式来存储数据,对消息的版本兼容性非常强:还有一个比较大的优点就是有着很 ...
随机推荐
- centos6 查看SELinux状态 关闭SELinux
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.在这种访问控制体系的限制下,进程只能访问那 ...
- Spring Boot 入门之持久层篇(三)
原文地址:Spring Boot 入门之持久层篇(三) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之 Web 篇(二)>介绍了 ...
- 从汇编的角度看待变量类型与sizeof的机制
1.动机:前段时间,一直有个疑问,就是编译器是从哪里知道数据的类型的,数据的类型是存在内存里面的么,因为自己调试编译器,发现内存中并没有多余的数据,后来在群上发问,才知道数据在编译成汇编的过程就知道数 ...
- OPCDAAuto.dll 的一个坑
最近项目需要对SCADA系统的下位机采集实时数据,常见做法是两种,一种采用ModBus RTU/TCP协议直接通过支持ModBus的下位机通信,一种是通过OPC规范,使用厂商提供的OPC Server ...
- python is 和 == 的区别
一.is 和 == 的区别 == 比较 比较的俩边的值 is 比较 比较的是内存地址 id() 二.小数据池 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址就不一样 字符串 ...
- CentOS6.4 上搭建NIS网络信息服务器
NIS(Network Information Service)网络信息服务,主要功能是提供用户登录信息给客户端主机查询之用,用于企业局域网Linux主机账户的集中管理(非跨平台).NIS服务器在大型 ...
- Shell教程快速入门
Shell即是一种命令语言,又是一种程序设计语言,使用者可以通过Shell访问操作系统的内核服务. Shell编程和java.python.C一样,只要一个能编写代码的文本编辑器和一个能解释执行的脚本 ...
- StringsUtil字符串工具类---灵活截取
package com.js.ai.modules.pointwall.interfac; import javax.print.attribute.standard.MediaName; publi ...
- oracle 索引使用小结
1. 普通索引 create index my_index on test (col_1); 可创建合并两列或多列的索引,最多可将32列合并在一个索引中(位图索引最多可合并30列) create in ...
- apache DOCUMENT_ROOT
问题描述:本地页面错误,+1上正常 本地及+1apache配置 <VirtualHost *:> ServerAdmin webmaster@dummy-host.example.com ...