golang中protobuf使用

本文介绍了如何在Golang中使用protobuf进行序列化和反序列化操作。首先,需要从GitHub获取protobuf的golang包,然后利用protoc工具将proto格式的文件转换为Go的struct类型。通过这些步骤,可以实现数据的高效序列化和反序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在golang中protobuf的功能主要就是序列化与反序列化两种操作,这两种操作的方法在第三方的包里面都有。

首先引用protobuf,从github上获得相应的包。地址"github.com/golang/protobuf/proto"

在golang中需要先把proto格式的文件使用protoc转换成对应的go的struct类型。

proto文件:

syntax="proto2";
package nxin;

message Nxin{
    required int32 id=1;
    required string name=2;
    required string telphone=3;
}
需要注意的是syntax为proto2,与proto3不同。3中不需要前面的required,并且3生成的struct赋值时不能使用proto.string()这种格式。

转换后的go文件:

// Code generated by protoc-gen-go. DO NOT EDIT.
// source: nxin.proto

/*
Package nxin is a generated protocol buffer package.

It is generated from these files:
	nxin.proto

It has these top-level messages:
	Nxin
*/
package nxin

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package

type Nxin struct {
	Id               *int32  `protobuf:"varint,1,req,name=id" json:"id,omitempty"`
	Name             *string `protobuf:"bytes,2,req,name=name" json:"name,omitempty"`
	Telphone         *string `protobuf:"bytes,3,req,name=telphone" json:"telphone,omitempty"`
	XXX_unrecognized []byte  `json:"-"`
}

func (m *Nxin) Reset()                    { *m = Nxin{} }
func (m *Nxin) String() string            { return proto.CompactTextString(m) }
func (*Nxin) ProtoMessage()               {}
func (*Nxin) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }

func (m *Nxin) GetId() int32 {
	if m != nil && m.Id != nil {
		return *m.Id
	}
	return 0
}

func (m *Nxin) GetName() string {
	if m != nil && m.Name != nil {
		return *m.Name
	}
	return ""
}

func (m *Nxin) GetTelphone() string {
	if m != nil && m.Telphone != nil {
		return *m.Telphone
	}
	return ""
}

func init() {
	proto.RegisterType((*Nxin)(nil), "nxin.Nxin")
}

func init() { proto.RegisterFile("nxin.proto", fileDescriptor0) }

var fileDescriptor0 = []byte{
	// 98 bytes of a gzipped FileDescriptorProto
	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0xca, 0xab, 0xc8, 0xcc,
	0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x01, 0xb1, 0x95, 0xdc, 0xb8, 0x58, 0xfc, 0x2a,
	0x32, 0xf3, 0x84, 0xf8, 0xb8, 0x98, 0x32, 0x53, 0x24, 0x18, 0x15, 0x98, 0x34, 0x58, 0x83, 0x98,
	0x32, 0x53, 0x84, 0x84, 0xb8, 0x58, 0xf2, 0x12, 0x73, 0x53, 0x25, 0x98, 0x14, 0x98, 0x34, 0x38,
	0x83, 0xc0, 0x6c, 0x21, 0x29, 0x2e, 0x8e, 0x92, 0xd4, 0x9c, 0x82, 0x8c, 0xfc, 0xbc, 0x54, 0x09,
	0x66, 0xb0, 0x38, 0x9c, 0x0f, 0x08, 0x00, 0x00, 0xff, 0xff, 0x49, 0x3f, 0x8a, 0xdd, 0x5a, 0x00,
	0x00, 0x00,
}

主要操作:

	msg:=&model.Helloworld{10,"wang","beijing"}
	buffer,_:=proto.Marshal(msg)//序列化
	fmt.Print(buffer)
	m2:=&model.Helloworld{}
	proto.Unmarshal(buffer,m2)//反序列化
	fmt.Println(m2)

	nx:=&nxin.Nxin{Id:proto.Int32(23),Name:proto.String("nxin"),Telphone:proto.String("18311175181")}
	b2,_:=proto.Marshal(nx)
	f,_:=os.Create("D:/nx.txt")
	defer f.Close()
	f.Write(b2)

序列化以后数据被转换成二进制类型,反序列化后直接写入对应的结构体类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值