golang中Protobuf如何使用
这篇文章给大家介绍golang中Protobuf如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
成都创新互联公司专注于三河企业网站建设,自适应网站建设,商城网站建设。三河网站建设公司,为三河等地区提供建站服务。全流程按需定制开发,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
protobuf是一种高效的数据传输格式(Google's data interchange format),且与语言无关,protobuf和json是基于http服务中最常见的两种数据格式。今天来学习基于golang的protobuf相关内容。
要想使用protobuf,需要4步:
下载安装google protocol buffer 编译器:https://github.com/google/protobuf/releases/tag/v3.5.1
下载安装golang protobuf plugin:https://github.com/golang/protobuf
使用go tools安装:go get -u github.com/golang/protobuf/protoc-gen-go按照protobuf 语法规则编写.proto文件
将proto文件编译成golang代码模型
protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/addressbook.proto
-IPATH, --proto_path=PATH 指定import路径
--go_out 指定生成go代码路径(同理--java_out是指定java生成路径)
下面以一个实际例子来说明:
addressbook.proto
syntax = "proto3"; package test.protobuf.tutorial; message Person{ string name = 1;//姓名 int32 id = 2;//id编号 string email = 3;//邮箱 enum PhoneType { //枚举类型(电话类型) MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber{ string number = 1; PhoneType type = 2; } repeated PhoneNumber phones = 4; //repeated可理解为动态数组 } // Our address book file is just one of these. message AddressBook { repeated Person people = 1; }
编译生成go文件。protoc会调用protoc-go-gen来生成go文件
.
├── addressbook.proto
└── src
└── addressbook.pb.go
protoc --go_out=./src addressbook.proto
addressbook.pb.go
// Code generated by protoc-gen-go. DO NOT EDIT. // source: addressbook.proto /* Package test_protobuf_tutorial is a generated protocol buffer package. It is generated from these files: addressbook.proto It has these top-level messages: Person AddressBook */ package test_protobuf_tutorial 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 Person_PhoneType int32 const ( Person_MOBILE Person_PhoneType = 0 Person_HOME Person_PhoneType = 1 Person_WORK Person_PhoneType = 2 ) var Person_PhoneType_name = map[int32]string{ 0: "MOBILE", 1: "HOME", 2: "WORK", } var Person_PhoneType_value = map[string]int32{ "MOBILE": 0, "HOME": 1, "WORK": 2, } func (x Person_PhoneType) String() string { return proto.EnumName(Person_PhoneType_name, int32(x)) } func (Person_PhoneType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0, 0} } type Person struct { Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` Id int32 `protobuf:"varint,2,opt,name=id" json:"id,omitempty"` Email string `protobuf:"bytes,3,opt,name=email" json:"email,omitempty"` Phones []*Person_PhoneNumber `protobuf:"bytes,4,rep,name=phones" json:"phones,omitempty"` } func (m *Person) Reset() { *m = Person{} } func (m *Person) String() string { return proto.CompactTextString(m) } func (*Person) ProtoMessage() {} func (*Person) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } func (m *Person) GetName() string { if m != nil { return m.Name } return "" } func (m *Person) GetId() int32 { if m != nil { return m.Id } return 0 } func (m *Person) GetEmail() string { if m != nil { return m.Email } return "" } func (m *Person) GetPhones() []*Person_PhoneNumber { if m != nil { return m.Phones } return nil } type Person_PhoneNumber struct { Number string `protobuf:"bytes,1,opt,name=number" json:"number,omitempty"` Type Person_PhoneType `protobuf:"varint,2,opt,name=type,enum=test.protobuf.tutorial.Person_PhoneType" json:"type,omitempty"` } func (m *Person_PhoneNumber) Reset() { *m = Person_PhoneNumber{} } func (m *Person_PhoneNumber) String() string { return proto.CompactTextString(m) } func (*Person_PhoneNumber) ProtoMessage() {} func (*Person_PhoneNumber) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0, 0} } func (m *Person_PhoneNumber) GetNumber() string { if m != nil { return m.Number } return "" } func (m *Person_PhoneNumber) GetType() Person_PhoneType { if m != nil { return m.Type } return Person_MOBILE } // Our address book file is just one of these. type AddressBook struct { People []*Person `protobuf:"bytes,1,rep,name=people" json:"people,omitempty"` } func (m *AddressBook) Reset() { *m = AddressBook{} } func (m *AddressBook) String() string { return proto.CompactTextString(m) } func (*AddressBook) ProtoMessage() {} func (*AddressBook) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } func (m *AddressBook) GetPeople() []*Person { if m != nil { return m.People } return nil } func init() { proto.RegisterType((*Person)(nil), "test.protobuf.tutorial.Person") proto.RegisterType((*Person_PhoneNumber)(nil), "test.protobuf.tutorial.Person.PhoneNumber") proto.RegisterType((*AddressBook)(nil), "test.protobuf.tutorial.AddressBook") proto.RegisterEnum("test.protobuf.tutorial.Person_PhoneType", Person_PhoneType_name, Person_PhoneType_value) } func init() { proto.RegisterFile("addressbook.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ // 265 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x90, 0xc1, 0x4b, 0xbc, 0x50, 0x10, 0xc7, 0x7f, 0xba, 0xee, 0xe3, 0xb7, 0x23, 0x2c, 0x36, 0xc4, 0x22, 0x1d, 0x42, 0x3c, 0x49, 0x81, 0x87, 0x0d, 0x3a, 0x75, 0x49, 0x10, 0x8a, 0xda, 0x5c, 0x1e, 0x41, 0x67, 0xcd, 0x89, 0x64, 0xd5, 0x79, 0xe8, 0xf3, 0xb0, 0xff, 0x49, 0x7f, 0x6e, 0xf8, 0x94, 0xe8, 0x10, 0xd1, 0xed, 0x3b, 0xc3, 0x67, 0xf8, 0xcc, 0x0c, 0x9c, 0xe4, 0x65, 0xd9, 0x51, 0xdf, 0x17, 0xcc, 0x87, 0x58, 0x75, 0xac, 0x19, 0x37, 0x9a, 0x7a, 0x3d, 0xe5, 0x62, 0x78, 0x8b, 0xf5, 0xa0, 0xb9, 0xab, 0xf2, 0x3a, 0xfc, 0xb0, 0x41, 0xec, 0xa9, 0xeb, 0xb9, 0x45, 0x04, 0xa7, 0xcd, 0x1b, 0xf2, 0xad, 0xc0, 0x8a, 0x56, 0xd2, 0x64, 0x5c, 0x83, 0x5d, 0x95, 0xbe, 0x1d, 0x58, 0xd1, 0x52, 0xda, 0x55, 0x89, 0xa7, 0xb0, 0xa4, 0x26, 0xaf, 0x6a, 0x7f, 0x61, 0xa0, 0xa9, 0xc0, 0x04, 0x84, 0x7a, 0xe7, 0x96, 0x7a, 0xdf, 0x09, 0x16, 0x91, 0xbb, 0xbd, 0x88, 0x7f, 0xb6, 0xc5, 0x93, 0x29, 0xde, 0x8f, 0xf0, 0xd3, 0xd0, 0x14, 0xd4, 0xc9, 0x79, 0xf2, 0xec, 0x15, 0xdc, 0x6f, 0x6d, 0xdc, 0x80, 0x68, 0x4d, 0x9a, 0xd7, 0x99, 0x2b, 0xbc, 0x01, 0x47, 0x1f, 0x15, 0x99, 0x95, 0xd6, 0xdb, 0xe8, 0x2f, 0xa2, 0xe7, 0xa3, 0x22, 0x69, 0xa6, 0xc2, 0x4b, 0x58, 0x7d, 0xb5, 0x10, 0x40, 0xec, 0xb2, 0xe4, 0xfe, 0x31, 0xf5, 0xfe, 0xe1, 0x7f, 0x70, 0xee, 0xb2, 0x5d, 0xea, 0x59, 0x63, 0x7a, 0xc9, 0xe4, 0x83, 0x67, 0x87, 0x29, 0xb8, 0xb7, 0xd3, 0x1f, 0x13, 0xe6, 0x03, 0x5e, 0x83, 0x50, 0xc4, 0xaa, 0x1e, 0x1f, 0x34, 0x1e, 0x79, 0xfe, 0xbb, 0x5b, 0xce, 0x74, 0x21, 0x0c, 0x71, 0xf5, 0x19, 0x00, 0x00, 0xff, 0xff, 0x38, 0xba, 0x26, 0x8c, 0x95, 0x01, 0x00, 0x00, }
ok,生成模型代码后,我们就可以使用protobuf协议来对数据进行编解码了
//将数据编码成protobuf 二进制格式--Writing a Message func EncodeAddressBook (book *pb.AddressBook)(out []byte, err error) { // Write the new address book back to disk. out, err = proto.Marshal(book) if err != nil { log.Println("Failed to encode address book:", err) return nil,err } if err := ioutil.WriteFile("addressbook_proto.data", out, 0644); err != nil { log.Fatalln("Failed to write address book:", err) return nil,err } fmt.Println("[Debug]: ", out) return }
//将二进制数据解码为golang 数据结构 --Reading a Message func DecodeAddressBook(in []byte) (book *pb.AddressBook, err error){ // Read the existing address book. book = &pb.AddressBook{} if err := proto.Unmarshal(in, book); err != nil { log.Println("Failed to parse address book:", err) return nil,err } return }
关于golang中Protobuf如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网页标题:golang中Protobuf如何使用
文章地址:http://pwwzsj.com/article/ighjic.html