golang与protobuf整合(应用)

神奇的考拉 · · 929 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

安装

在go中使用protobuf,有两个可选用的包goprotobuf(go官方出品)和gogoprotobuf。
gogoprotobuf完全兼容google protobuf,它生成的代码质量和编解码性能均比goprotobuf高一些

安装protoc

首先去下载protobuf的编译器protoc ,windows上可以直接下到exe文件(linux则需要编译),最后将下载好的可执行文件拷贝到GOPATH的bin目录下(GOPATH/bin目录最好添加到系统环境变量里)

安装protobuf库文件

go get github.com/golang/protobuf/proto

goprotobuf

安装插件

go get github.com/golang/protobuf/protoc-gen-go

实例demo

/ 指定版本
// 不同版本写法存在差异
syntax = "proto3";

// 包名,通过protoc生成go文件时
package test;

// 手机类型
// 指定字段顺序 第一个必须为0
enum PhoneType{
     HOME = 0;
     WORK = 1;
}

// 手机
message Phone{
    PhoneType type = 1;
    string number  = 2;
}

// 人
message Person{
    int32 id = 1;
    string name = 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文件时

package testprotobuf

import (
    fmt "fmt"
    proto "github.com/golang/protobuf/proto"
    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.ProtoPackageIsVersion3 // please upgrade the proto package

// 手机类型
// 指定字段顺序 第一个必须为0
type PhoneType int32

const (
    PhoneType_HOME PhoneType = 0
    PhoneType_WORK PhoneType = 1
)

var PhoneType_name = map[int32]string{
    0: "HOME",
    1: "WORK",
}

var PhoneType_value = map[string]int32{
    "HOME": 0,
    "WORK": 1,
}

func (x PhoneType) String() string {
    return proto.EnumName(PhoneType_name, int32(x))
}

func (PhoneType) EnumDescriptor() ([]byte, []int) {
    return fileDescriptor_c161fcfdc0c3ff1e, []int{0}
}

// 手机
type Phone struct {
    Type                 PhoneType `protobuf:"varint,1,opt,name=type,proto3,enum=testprotobuf.PhoneType" json:"type,omitempty"`
    Number               string    `protobuf:"bytes,2,opt,name=number,proto3" json:"number,omitempty"`
    XXX_NoUnkeyedLiteral struct{}  `json:"-"`
    XXX_unrecognized     []byte    `json:"-"`
    XXX_sizecache        int32     `json:"-"`
}

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

func (m *Phone) XXX_Unmarshal(b []byte) error {
    return xxx_messageInfo_Phone.Unmarshal(m, b)
}
func (m *Phone) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
    return xxx_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 {
    return xxx_messageInfo_Phone.Size(m)
}
func (m *Phone) XXX_DiscardUnknown() {
    xxx_messageInfo_Phone.DiscardUnknown(m)
}

var xxx_messageInfo_Phone proto.InternalMessageInfo

func (m *Phone) GetType() PhoneType {
    if m != nil {
        return m.Type
    }
    return PhoneType_HOME
}

func (m *Phone) GetNumber() string {
    if m != nil {
        return m.Number
    }
    return ""
}

// 人
type Person struct {
    Id   int32  `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
    Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
    // repeated表示重复
    Phones               []*Phone `protobuf:"bytes,3,rep,name=phones,proto3" json:"phones,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-"`
    XXX_unrecognized     []byte   `json:"-"`
    XXX_sizecache        int32    `json:"-"`
}

func (m *Person) Reset()         { *m = Person{} }
func (m *Person) String() string { return proto.CompactTextString(m) }
func (*Person) ProtoMessage()    {}
func (*Person) Descriptor() ([]byte, []int) {
    return fileDescriptor_c161fcfdc0c3ff1e, []int{1}
}

func (m *Person) XXX_Unmarshal(b []byte) error {
    return xxx_messageInfo_Person.Unmarshal(m, b)
}
func (m *Person) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
    return xxx_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 {
    return xxx_messageInfo_Person.Size(m)
}
func (m *Person) XXX_DiscardUnknown() {
    xxx_messageInfo_Person.DiscardUnknown(m)
}

var xxx_messageInfo_Person proto.InternalMessageInfo

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

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

func (m *Person) GetPhones() []*Phone {
    if m != nil {
        return m.Phones
    }
    return nil
}

type ContactBook struct {
    Persons              []*Person `protobuf:"bytes,1,rep,name=persons,proto3" json:"persons,omitempty"`
    XXX_NoUnkeyedLiteral struct{}  `json:"-"`
    XXX_unrecognized     []byte    `json:"-"`
    XXX_sizecache        int32     `json:"-"`
}

func (m *ContactBook) Reset()         { *m = ContactBook{} }
func (m *ContactBook) String() string { return proto.CompactTextString(m) }
func (*ContactBook) ProtoMessage()    {}
func (*ContactBook) Descriptor() ([]byte, []int) {
    return fileDescriptor_c161fcfdc0c3ff1e, []int{2}
}

func (m *ContactBook) XXX_Unmarshal(b []byte) error {
    return xxx_messageInfo_ContactBook.Unmarshal(m, b)
}
func (m *ContactBook) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
    return xxx_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 {
    return xxx_messageInfo_ContactBook.Size(m)
}
func (m *ContactBook) XXX_DiscardUnknown() {
    xxx_messageInfo_ContactBook.DiscardUnknown(m)
}

var xxx_messageInfo_ContactBook proto.InternalMessageInfo

func (m *ContactBook) GetPersons() []*Person {
    if m != nil {
        return m.Persons
    }
    return nil
}

func init() {
    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")
}

func init() { proto.RegisterFile("test.proto", fileDescriptor_c161fcfdc0c3ff1e) }

var fileDescriptor_c161fcfdc0c3ff1e = []byte{
    // 221 bytes of a gzipped FileDescriptorProto
    0x1f, 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,
}

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:神奇的考拉

查看原文:golang与protobuf整合(应用)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

929 次点击  
加入收藏 微博
1 回复  |  直到 2019-02-18 17:53:17
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传