当前位置: 首页 > news >正文

golang与protobuf

在这里插入图片描述
需要配置环境变量
编写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编译
在这里插入图片描述
golang的测试代码:

  package main
    
    import (
    	"log"
    	// 辅助库
    	"github.com/golang/protobuf/proto"
    	// test.pb.go 的路径
    
    	"foodemo"
    	"fmt"
    )
    
    func main() {
    	fmt.Println("hello")
    	// 创建一个消息 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())
    	}else{
    		fmt.Println("the same")
    	}
    }

在这里插入图片描述

官方链接https://github.com/protocolbuffers/protobuf/blob/master/src/README.md

1.检查安装需要用到的编译工具
$ sudo apt-get install autoconf automake libtool curl make g++ unzip 
2.生成需要的配置脚本
$ ./autogen.sh
3.编译安装
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.

编译好的插件要放置于 GOPATH/bin下,GOPATH/bin下,GOPATH/bin 应该被加入 PATH 环境变量,以便 protoc 能够找到 protoc-gen-go

go get github.com/golang/protobuf/protoc-gen-go
进入下载好的目录
go build

获取 goprotobuf 提供的支持库,包含诸如编码(marshaling)、解码(unmarshaling)等功能
go get github.com/golang/protobuf/proto
进入下载好的目录
go build
go install

相关文章:

  • protobuf在ubuntu安装
  • protobuf中数据格式与类型
  • potobuf生成文件
  • GO get下载困难问题
  • c语言二叉树
  • 丹华资本与区块链
  • hyperledger生成证书命令
  • crypto-config.yaml
  • 纯净版crypto-config.yaml文件
  • hyperledger生成peer和order
  • golang实现web服务器
  • ActiveMQ消息队列
  • export对环境变量进行设置
  • hyperledger中基本网络搭建示例
  • hyperledger中docker-compose文件示例
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • 4个实用的微服务测试策略
  • css系列之关于字体的事
  • Fabric架构演变之路
  • python学习笔记-类对象的信息
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • windows下mongoDB的环境配置
  • 对象引论
  • 关于springcloud Gateway中的限流
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 讲清楚之javascript作用域
  • 排序算法学习笔记
  • 如何学习JavaEE,项目又该如何做?
  • 微信小程序填坑清单
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 阿里云服务器如何修改远程端口?
  • #Linux(make工具和makefile文件以及makefile语法)
  • #QT(智能家居界面-界面切换)
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (0)Nginx 功能特性
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (附源码)springboot教学评价 毕业设计 641310
  • (论文阅读11/100)Fast R-CNN
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (四)汇编语言——简单程序
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (一)为什么要选择C++
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .NET Core 中的路径问题
  • .Net Core与存储过程(一)
  • .NET MVC之AOP
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .Net程序帮助文档制作
  • /etc/fstab和/etc/mtab的区别
  • @RequestBody的使用