1. 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
  2. cd protobuf-2.6.1
    ./configure
  3. make
  4. make install
  5. protoc   -h
  1. go get github.com/golang/protobuf/protoc-gen-go
  2.  
  3. cd github.com/golang/protobuf/protoc-gen-go
  4.  
  5. go build
  6.  
  7. go install
  8.  
  9. vi /etc/profile $GOPATH/bin 加入环境变量
  10.  
  11. source profile
  1. go get github.com/golang/protobuf/proto
  2. cd github.com/golang/protobuf/proto
  3. go build
  4. go install

使用 goprotobuf
这里通过一个例子来说明用法。先创建一个 .proto 文件 test.proto:

  1. package example;
  2.  
  3. enum FOO { X = 17; };
  4.  
  5. message Test {
  6. required string label = 1;
  7. optional int32 type = 2 [default=77];
  8. repeated int64 reps = 3;
  9. optional group OptionalGroup = 4 {
  10. required string RequiredField = 5;
  11. }
  12. }

编译此 .proto 文件:

  1. protoc --go_out=. *.proto

这里通过 –go_out 来使用 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go。这时候我们会生成一个名为 test.pb.go 的源文件。

在使用之前,我们先了解一下每个 Protobuf 消息在 Golang 中有哪一些可用的接口:

  1. 每一个 Protobuf 消息对应一个 Golang 结构体

  2. 消息中域名字为 camel_case 在对应的 Golang 结构体中为 CamelCase

  3. 消息对应的 Golang 结构体中不存在 setter 方法,只需要直接对结构体赋值即可,赋值时可能使用到一些辅助函数,例如:

    1. msg.Foo = proto.String("hello")
  4. 消息对应的 Golang 结构体中存在 getter 方法,用于返回域的值,如果域未设置值,则返回一个默认值

  5. 消息中非 repeated 的域都被实现为一个指针,指针为 nil 时表示域未设置

  6. 消息中 repeated 的域被实现为 slice

  7. 访问枚举值时,使用“枚举类型名_枚举名”的格式(更多内容可以直接阅读生成的源码)

  8. 使用 proto.Marshal 函数进行编码,使用 proto.Unmarshal 函数进行解码

现在我们编写一个小程序:

  1. package main
  2.  
  3. import (    "log"
  4.     // 辅助库
  5.     "github.com/golang/protobuf/proto"
  6.     // test.pb.go 的路径
  7.     "example")
  8.  
  9. func main() {    // 创建一个消息 Test
  10.     test := &example.Test{        // 使用辅助函数设置域的值
  11.         Label: proto.String("hello"),
  12.         Type:  proto.Int32(17),
  13.         Optionalgroup: &example.Test_OptionalGroup{
  14.             RequiredField: proto.String("good bye"),
  15.         },
  16.     }    // 进行编码
  17.     data, err := proto.Marshal(test)    
      if err != nil {
  18.         log.Fatal("marshaling error: ", err)
  19.     }    // 进行解码
  20.     newTest := &example.Test{}
  21.     err = proto.Unmarshal(data, newTest)    
      if err != nil {
  22.         log.Fatal("unmarshaling error: ", err)
  23.     }    // 测试结果
  24.     if test.GetLabel() != newTest.GetLabel() {
  25.         log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
  26.     }
  27. }

在 Golang 中使用 Protobuf的更多相关文章

  1. google的grpc在golang中的使用

    GRPC是google开源的一个高性能.跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,基于Netty 4.x. 前面写过一篇golang标准库的rpc包的用法,这篇文章接着讲一 ...

  2. python golang中grpc 使用示例代码详解

    python 1.使用前准备,安装这三个库 pip install grpcio pip install protobuf pip install grpcio_tools 2.建立一个proto文件 ...

  3. 【新手笔记】golang中使用protocol buffers 3

    主要参考了这篇帖子:https://segmentfault.com/a/1190000009277748 1.下载windows版本的PB https://github.com/protocolbu ...

  4. golang中的rpc开发

    golang中实现RPC非常简单,官方提供了封装好的库,还有一些第三方的库 golang官方的net/rpc库使用encoding/gob进行编解码,支持tcp和http数据传输方式,由于其他语言不支 ...

  5. 在Wcf中应用ProtoBuf替代默认的序列化器

    Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...

  6. webapi 中使用 protobuf

    相比json来说,好处是速度更快,带宽占用更小.其效果大致等于json+Gzip. 在webapi中使用protobuf的方法为: 引用nuget包 Install-Package protobuf- ...

  7. golang中的race检测

    golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...

  8. 基础知识 - Golang 中的正则表达式

    ------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...

  9. 在网络通讯中应用Protobuf

    在网络通讯中应用Protobuf Protobuf的设计非常适用于在网络通讯中的数据载体,它序列化出来的数据量少再加上以K-V的方式来存储数据,对消息的版本兼容性非常强:还有一个比较大的优点就是有着很 ...

随机推荐

  1. centos6 查看SELinux状态 关闭SELinux

    SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.在这种访问控制体系的限制下,进程只能访问那 ...

  2. Spring Boot 入门之持久层篇(三)

    原文地址:Spring Boot 入门之持久层篇(三) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之 Web 篇(二)>介绍了 ...

  3. 从汇编的角度看待变量类型与sizeof的机制

    1.动机:前段时间,一直有个疑问,就是编译器是从哪里知道数据的类型的,数据的类型是存在内存里面的么,因为自己调试编译器,发现内存中并没有多余的数据,后来在群上发问,才知道数据在编译成汇编的过程就知道数 ...

  4. OPCDAAuto.dll 的一个坑

    最近项目需要对SCADA系统的下位机采集实时数据,常见做法是两种,一种采用ModBus RTU/TCP协议直接通过支持ModBus的下位机通信,一种是通过OPC规范,使用厂商提供的OPC Server ...

  5. python is 和 == 的区别

    一.is 和 == 的区别 == 比较 比较的俩边的值 is 比较 比较的是内存地址 id() 二.小数据池 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址就不一样 字符串 ...

  6. CentOS6.4 上搭建NIS网络信息服务器

    NIS(Network Information Service)网络信息服务,主要功能是提供用户登录信息给客户端主机查询之用,用于企业局域网Linux主机账户的集中管理(非跨平台).NIS服务器在大型 ...

  7. Shell教程快速入门

    Shell即是一种命令语言,又是一种程序设计语言,使用者可以通过Shell访问操作系统的内核服务. Shell编程和java.python.C一样,只要一个能编写代码的文本编辑器和一个能解释执行的脚本 ...

  8. StringsUtil字符串工具类---灵活截取

    package com.js.ai.modules.pointwall.interfac; import javax.print.attribute.standard.MediaName; publi ...

  9. oracle 索引使用小结

    1. 普通索引 create index my_index on test (col_1); 可创建合并两列或多列的索引,最多可将32列合并在一个索引中(位图索引最多可合并30列) create in ...

  10. apache DOCUMENT_ROOT

    问题描述:本地页面错误,+1上正常 本地及+1apache配置 <VirtualHost *:> ServerAdmin webmaster@dummy-host.example.com ...