• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    在go语言中安装与使用protobuf的方法详解
    POST TIME:2021-10-18 17:39

    简介

    本文主要给大家介绍了关于go语言安装使用protobuf的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

    protobuf是Google开发出来的一个语言无关、平台无关的数据序列化工具,在rpc或tcp通信等很多场景都可以使用。通俗来讲,如果客户端和服务端使用的是不同的语言,那么在服务端定义一个数据结构,通过protobuf转化为字节流,再传送到客户端解码,就可以得到对应的数据结构。这就是protobuf神奇的地方。并且,它的通信效率极高,“一条消息数据,用protobuf序列化后的大小是json的10分之一,xml格式的20分之一,是二进制序列化的10分之一”。

    安装

    编译安装protobuf的编译器protoc

      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 查看安装是否成功

    安装插件 protoc-gen-go,它是一个go程序,编译它之后将可执行文件执行路径写入环境变量

    go get github.com/golang/protobuf/protoc-gen-go

    获取proto包

    go get github.com/golang/protobuf/proto

    在go中使用

    protobuf的使用方法是将数据结构写入到.proto文件中,使用protoc编译器编译(间接使用了插件)得到一个新的go包,里面包含go中可以使用的数据结构和一些辅助方法。

    编写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;
      }
     }

        编译:

        执行 protoc --go_out=. *.proto 生成 test.pb.go 文件

        将test.pb.go文件放入example文件夹(对应上面package)中,作为example包

    try

     package main
    
     import (
      "log"
    
      "github.com/golang/protobuf/proto"
      "example"
     )
    
     func main() {
      test := example.Test {
       Label: proto.String("hello"),
       Type: proto.Int32(17),
       Reps: []int64{1, 2, 3},
       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)
      }
      // Now test and newTest contain the same data.
      if test.GetLabel() != newTest.GetLabel() {
       log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
      }
      //test.GetOptionalgroup().GetRequiredField()
      //etc
     }

    一些对应关系

    这些只是一些特性,想要仔细研究可以查看github上的wiki:https://github.com/golang/protobuf

    总结

    以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

    您可能感兴趣的文章:
    • 详解vue中使用protobuf踩坑记
    • vue中使用protobuf的过程记录
    • Android中使用protobuf的具体示例
    • C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
    • Protobuf的简要介绍及使用详解
    上一篇:深入理解Golang的单元测试和性能测试
    下一篇:Go语言interface 与 nil 的比较
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信