golang Protobuf學習

   protobuf是一種高效的數據傳輸格式(Google's data interchange format),且與語言無關,protobuf和json是基於http服務中最多見的兩種數據格式。今天來學習基於golang的protobuf相關內容。java

google protocol buffer: https://developers.google.com/protocol-buffers/
golang 官方提供的protobuf支持插件:https://github.com/golang/protobufgit

要想使用protobuf,須要4步:github

  1. 下載安裝google protocol buffer 編譯器:https://github.com/google/protobuf/releases/tag/v3.5.1
  2. 下載安裝golang protobuf plugin:https://github.com/golang/protobuf
    使用go tools安裝:go get -u github.com/golang/protobuf/protoc-gen-go
  3. 按照protobuf 語法規則編寫.proto文件
  4. 將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.protogolang

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.protojson

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
}

proto buffer 3 指南:https://developers.google.com/protocol-buffers/docs/proto3
Go Generated Code Guide:https://developers.google.com/protocol-buffers/docs/reference/go-generatedide

相關文章
相關標籤/搜索