文章来源 https://www.jianshu.com/p/d189b205ae7e
安装
在go中使用protobuf,有两个可选用的包goprotobuf(go官方出品)和gogoprotobuf。
gogoprotobuf完全兼容google protobuf,它生成的代码质量和编解码性能均比goprotobuf高一些
安装protoc
首先去下载protobuf的编译器protoc,windows上可以直接下到exe文件(linux则需要编译),最后将下载好的可执行文件拷贝到GOPATH的bin目录下(GOPATH/bin目录最好添加到系统环境变量里)
安装protobuf库文件
gogetgithub.com/golang/protobuf/proto
goprotobuf
安装插件
goget github.com/golang/protobuf/protoc-gen-go
实例demo
/指定版本// 不同版本写法存在差异syntax="proto3";// 包名,通过protoc生成go文件时packagetest;// 手机类型// 指定字段顺序 第一个必须为0enum PhoneType{HOME=0;WORK=1;}// 手机message Phone{PhoneTypetype=1;stringnumber=2;}// 人message Person{int32id=1;stringname=2;// repeated表示重复repeated Phone phones=3;}message ContactBook{repeated Person persons=1;}
生成go文件
protoc --go_out=. *.proto
生成后的文件:
// Code generated by protoc-gen-go. DO NOT EDIT.// source: test.proto// 包名,通过protoc生成go文件时packagetestprotobufimport(fmt"fmt"proto"github.com/golang/protobuf/proto"math"math")// Reference imports to suppress errors if they are not otherwise used.var_=proto.Marshalvar_=fmt.Errorfvar_=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.ProtoPackageIsVersion3// please upgrade the proto package// 手机类型// 指定字段顺序 第一个必须为0typePhoneTypeint32const(PhoneType_HOME PhoneType=0PhoneType_WORK PhoneType=1)varPhoneType_name=map[int32]string{0:"HOME",1:"WORK",}varPhoneType_value=map[string]int32{"HOME":0,"WORK":1,}func(x PhoneType)String()string{returnproto.EnumName(PhoneType_name,int32(x))}func(PhoneType)EnumDescriptor()([]byte,[]int){returnfileDescriptor_c161fcfdc0c3ff1e,[]int{0}}// 手机typePhonestruct{Type PhoneType`protobuf:"varint,1,opt,name=type,proto3,enum=testprotobuf.PhoneType" json:"type,omitempty"`Numberstring`protobuf:"bytes,2,opt,name=number,proto3" json:"number,omitempty"`XXX_NoUnkeyedLiteralstruct{}`json:"-"`XXX_unrecognized[]byte`json:"-"`XXX_sizecacheint32`json:"-"`}func(m*Phone)Reset(){*m=Phone{}}func(m*Phone)String()string{returnproto.CompactTextString(m)}func(*Phone)ProtoMessage(){}func(*Phone)Descriptor()([]byte,[]int){returnfileDescriptor_c161fcfdc0c3ff1e,[]int{0}}func(m*Phone)XXX_Unmarshal(b[]byte)error{returnxxx_messageInfo_Phone.Unmarshal(m,b)}func(m*Phone)XXX_Marshal(b[]byte,deterministicbool)([]byte,error){returnxxx_messageInfo_Phone.Marshal(b,m,deterministic)}func(m*Phone)XXX_Merge(src proto.Message){xxx_messageInfo_Phone.Merge(m,src)}func(m*Phone)XXX_Size()int{returnxxx_messageInfo_Phone.Size(m)}func(m*Phone)XXX_DiscardUnknown(){xxx_messageInfo_Phone.DiscardUnknown(m)}varxxx_messageInfo_Phone proto.InternalMessageInfofunc(m*Phone)GetType()PhoneType{ifm!=nil{returnm.Type}returnPhoneType_HOME}func(m*Phone)GetNumber()string{ifm!=nil{returnm.Number}return""}// 人typePersonstruct{Idint32`protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`Namestring`protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`// repeated表示重复Phones[]*Phone`protobuf:"bytes,3,rep,name=phones,proto3" json:"phones,omitempty"`XXX_NoUnkeyedLiteralstruct{}`json:"-"`XXX_unrecognized[]byte`json:"-"`XXX_sizecacheint32`json:"-"`}func(m*Person)Reset(){*m=Person{}}func(m*Person)String()string{returnproto.CompactTextString(m)}func(*Person)ProtoMessage(){}func(*Person)Descriptor()([]byte,[]int){returnfileDescriptor_c161fcfdc0c3ff1e,[]int{1}}func(m*Person)XXX_Unmarshal(b[]byte)error{returnxxx_messageInfo_Person.Unmarshal(m,b)}func(m*Person)XXX_Marshal(b[]byte,deterministicbool)([]byte,error){returnxxx_messageInfo_Person.Marshal(b,m,deterministic)}func(m*Person)XXX_Merge(src proto.Message){xxx_messageInfo_Person.Merge(m,src)}func(m*Person)XXX_Size()int{returnxxx_messageInfo_Person.Size(m)}func(m*Person)XXX_DiscardUnknown(){xxx_messageInfo_Person.DiscardUnknown(m)}varxxx_messageInfo_Person proto.InternalMessageInfofunc(m*Person)GetId()int32{ifm!=nil{returnm.Id}return0}func(m*Person)GetName()string{ifm!=nil{returnm.Name}return""}func(m*Person)GetPhones()[]*Phone{ifm!=nil{returnm.Phones}returnnil}typeContactBookstruct{Persons[]*Person`protobuf:"bytes,1,rep,name=persons,proto3" json:"persons,omitempty"`XXX_NoUnkeyedLiteralstruct{}`json:"-"`XXX_unrecognized[]byte`json:"-"`XXX_sizecacheint32`json:"-"`}func(m*ContactBook)Reset(){*m=ContactBook{}}func(m*ContactBook)String()string{returnproto.CompactTextString(m)}func(*ContactBook)ProtoMessage(){}func(*ContactBook)Descriptor()([]byte,[]int){returnfileDescriptor_c161fcfdc0c3ff1e,[]int{2}}func(m*ContactBook)XXX_Unmarshal(b[]byte)error{returnxxx_messageInfo_ContactBook.Unmarshal(m,b)}func(m*ContactBook)XXX_Marshal(b[]byte,deterministicbool)([]byte,error){returnxxx_messageInfo_ContactBook.Marshal(b,m,deterministic)}func(m*ContactBook)XXX_Merge(src proto.Message){xxx_messageInfo_ContactBook.Merge(m,src)}func(m*ContactBook)XXX_Size()int{returnxxx_messageInfo_ContactBook.Size(m)}func(m*ContactBook)XXX_DiscardUnknown(){xxx_messageInfo_ContactBook.DiscardUnknown(m)}varxxx_messageInfo_ContactBook proto.InternalMessageInfofunc(m*ContactBook)GetPersons()[]*Person{ifm!=nil{returnm.Persons}returnnil}funcinit(){proto.RegisterEnum("testprotobuf.PhoneType",PhoneType_name,PhoneType_value)proto.RegisterType((*Phone)(nil),"testprotobuf.Phone")proto.RegisterType((*Person)(nil),"testprotobuf.Person")proto.RegisterType((*ContactBook)(nil),"testprotobuf.ContactBook")}funcinit(){proto.RegisterFile("test.proto",fileDescriptor_c161fcfdc0c3ff1e)}varfileDescriptor_c161fcfdc0c3ff1e=[]byte{// 221 bytes of a gzipped FileDescriptorProto0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00,0x02,0xff,0x6c,0x8f,0x41,0x4b,0xc4,0x30,0x10,0x85,0x4d,0xb7,0x1b,0xdd,0x59,0x59,0x96,0x51,0x34,0x37,0x4b,0x4f,0xc5,0x42,0x0e,0xf5,0xec,0x45,0x11,0x04,0x95,0x96,0x20,0x88,0xc7,0xd6,0x46,0x2c,0xd2,0x24,0x34,0xe9,0xa1,0xff,0x5e,0x3a,0x54,0x11,0xf1,0xf6,0x85,0x7c,0xef,0x3d,0x06,0x20,0x68,0x1f,0xa4,0x1b,0x6c,0xb0,0x78,0x3c,0x33,0x61,0x33,0xbe,0xa7,0x8f,0xb0,0xae,0x3e,0xac,0xd1,0x98,0x43,0x1c,0x26,0xa7,0x05,0x4b,0x58,0xb6,0x2b,0xce,0xe5,0x6f,0x4b,0x92,0xf2,0x3c,0x39,0xad,0x48,0xc2,0x33,0xe0,0x66,0xec,0x1b,0x3d,0x88,0x28,0x61,0xd9,0x46,0x2d,0xaf,0xf4,0x15,0x78,0xa5,0x07,0x6f,0x0d,0xee,0x20,0xea,0x5a,0x2a,0x5b,0xab,0xa8,0x6b,0x11,0x21,0x36,0x75,0xaf,0x17,0x9f,0x18,0x73,0xe0,0x6e,0x2e,0xf6,0x62,0x95,0xac,0xb2,0x6d,0x71,0xf2,0xcf,0xa8,0x5a,0x94,0xf4,0x1a,0xb6,0xb7,0xd6,0x84,0xfa,0x2d,0xdc,0x58,0xfb,0x89,0x12,0x0e,0x1d,0x2d,0x79,0xc1,0x28,0x7c,0xfa,0x27,0x4c,0x9f,0xea,0x5b,0xba,0xbc,0x80,0xcd,0xcf,0x11,0x78,0x04,0xf1,0x7d,0xf9,0x74,0xb7,0x3f,0x98,0xe9,0xa5,0x54,0x0f,0x7b,0xd6,0x70,0x8a,0x5e,0x7d,0x05,0x00,0x00,0xff,0xff,0xa4,0x04,0x76,0xab,0x2b,0x01,0x00,0x00,}
作者:神奇的考拉
链接:https://www.jianshu.com/p/d189b205ae7e
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。