Go语言 ProtoBuf 序列化和反序列化

前言

Protobuf

是Google开发的一个网络通信协议,提供了高效率的序列化和反序列化机制,序列化就是把对象转换成二进制数据发送给服务端,反序列化就是将收到的二进制数据转换成对应的对象。官方版本支持Go,C++,Java,Python等语言。

Go语言中Protocol Buffers是一个灵活、高效的、自动化的用于对结构数据进行序列化和反序列化协议,与XML、Json相比,Protocol Buffers序列化后码流更小、速度理快、操作简单。

Protobuf优点

  • 体积小,效率高
  • 使用简单,兼容性好,维护简单
  • 加密性好
  • 跨平台

protoc版本

protoc --version
libprotoc 3.11.4

proto文件

syntax = "proto3";

package proto_jsonpb;

option go_package = "pb_models/pf";

message Doubles
{
    double doubles = 1;
}

message Maps
{
    map<string, string> maps = 1;
}

enum Status
{
    UNKNOWN = 0;
    RUNNING = 1;
    STOPED = 2;
}

message UnionMappy
{
    map<int64, int32> nums = 1;
    map<string, string> strs = 2;
    map<int32, Maps> impas = 3;
    map<bool, bool> bl = 4;
    map<string, Status> sts = 5;
    map<int32, bool> i32bl = 6;
    map<int64, bool> i64bl = 7;
}

代码实现

package main

import (
	"fmt"
	"github.com/golang/protobuf/proto"
	"github.com/gtest/pb_models/pf"
)

func main() {

	var um = &pf.UnionMappy{
		Nums: map[int64]int32{
			10: 1,
		},
		Strs: map[string]string{
			"str1": "val1",
		},
		Bl: map[bool]bool{
			true: true,
		},
		Sts: map[string]pf.Status{
			"status::0": pf.Status_UNKNOWN,
			"status::1": pf.Status_RUNNING,
			"status::2": pf.Status_STOPED,
		},
		I32Bl: map[int32]bool{
			30: true,
		},
		I64Bl: map[int64]bool{
			60: false,
		},
	}

	p, err := proto.Marshal(um)
	if err != nil {
		fmt.Println("marshaling err : ", err)
	}

	un := &pf.UnionMappy{}
	err = proto.Unmarshal(p, un)
	if err != nil {
		fmt.Println("unmarshal err : ", err)
	}

	fmt.Println("um.Nums:", um.GetNums(), ",um.Sts:", um.GetSts())

	fmt.Println("um地址:", &um, "\num pf:", um)
	fmt.Println("un地址:", &un, "\nun pf:", un)

	fmt.Println("un.Nums:", un.GetNums(), ",un.Sts:", un.GetSts())

	var dd = pf.Doubles{
		Doubles: 1.321,
	}
	fmt.Println("doubles double is  : ", dd.GetDoubles())

	fmt.Printf("status UNKNOWN:%s, status RUNNING:%s,status STOPED:%s\n",
		pf.Status_UNKNOWN, pf.Status_RUNNING, pf.Status_STOPED)
	fmt.Println("status name is : ", pf.Status_name)
	fmt.Println("status value is : ", pf.Status_value)

	return
}

运行结果

protoc --go_out=plugins=grpc:. pf.proto

发布了147 篇原创文章 · 获赞 18 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/guichenglin/article/details/105272861