golang Protobuf学习

protobuf是一种高效的数据传输格式(Google‘s data interchange format),且与语言无关,protobuf和json是基于http服务中最常见的两种数据格式。今天来学习基于golang的protobuf相关内容。

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

要想使用protobuf,需要4步:

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

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

原文地址:http://blog.51cto.com/xwandrew/2065575

时间: 2024-11-05 22:38:05

golang Protobuf学习的相关文章

golang protobuf SetExtension

对golang protobuf 的扩展字段赋值时候一直提示proto: bad extension value type clkUrl:="z.cn" proto.SetExtension(seatbid, rtb.E_Clkurl, clkUrl) 查看源码后发现,对扩展字段赋值的时候会对第二个参数进行反射获取类型,判断第三个参数的类型和第二个是否匹配 , func SetExtension(pb Message, extension *ExtensionDesc, value i

google protobuf学习笔记一:windows下环境配置

欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/45371743 protobuf的使用和原理,请查看:http://blog.csdn.net/majianfei1023/article/details/45112415 Windows下google protobuf开发环境配置 最近项目需求,Client与Server的网络通信协议传输使用google protobuf rpc.对于Protobuf,以前是只

golang http 学习

断断续续学习golang 已经有大半年了. 几次涉猎过golang net package, 去年也用过Beego,但是总觉得自己只知皮毛,不能精通.所以,这次誓将其一举拿下.达到知道其宏观组织结构,又了解实现细节,甚至设计时各种考虑以及折中. 目标树立好了,后面的就靠实践了.学习也要有计划,有条理.规划一个好的学习顺序和学习策略,可以节节高升,事半功倍. 那下面就简单规划一下学习路径.本着先易后难,既注重实用,又兼顾细节的原则,规划如下. 1. learn http package struc

protobuf学习资料整理

最近准备使用protobuf, 整理一下比较好的学习资料. 1. 官方文档: https://developers.google.com/protocol-buffers 有些内容是基于proto3的, 大部分情况下proto2也都适用 2. IBM developworks的入门文章: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ 3. 淘宝搜索技术博客的文章, 重点介绍protobuf的反射和自描述: http://www.sear

protobuf 学习笔记

它是什么 术语定义 protocol buffer 官网github定义:https://github.com/google/protobuf https://developers.google.com/protocol-buffers/  开发者帮助 百度百科定义:http://baike.baidu.com/link?url=vlbJtV9p9Vfj4wQDif_XaNymBZI67u3AFjF5ZpT6iJjOwGfsenwUZMieBGrtlUC0FkWwWQic-Y3rwXYP0tq9

golang基础学习及web框架

golang的web框架 web框架百花齐放:对比 Go Web 编程 Go Web Examples Golang 适合做 Web 开发吗? beego beego简介 go-restful golang学习 go语言圣经 go指南 go指南本地化: 中文版 go get -u github.com/Go-zh/tour tour 英文版 go get golang.org/x/tour tour go指南中文版 go知乎 go学习指南 原文地址:https://www.cnblogs.com

protobuf 学习 收藏的文章

Protobuf数据格式解析: packed repeated与repeated的区别在于编码方式不一样,repeated将多个属性类型与值分开存储.而packed repeated采用Length-delimited方式. proto3的repeated默认就是使用packed这种方式来存储,(proto2与proto3区别在于.proto的语法). http://blog.csdn.net/zhaozheng7758/article/details/6749000: [packed =fal

Golang基础学习总结

1.不支持继承 重载 ,比如C++Java的接口,接口的修改会影响整个实现改接口的类行为的修改,Go 设计者认为这一特点或许根本没用. 2.必任何函数定义必须花括号跟在函数声明后面而不能换行 如 func  funca(a int){},在Go语言中 函数也是一种类型 可以被推导  使用支持匿名函数 和闭包. 函数的返回值支持多重返回类似Python , 如果不赋值  整数类型默认 0     浮点数类型默认0.0   error类型 默认是nil 3.不用的包一定不要引入,这是Go的原则,就如

google protobuf学习笔记一:使用和原理

一.什么是protobuf protobuf全称Google Protocol Buffers,是google开发的的一套用于数据存储,网络通信时用于协议编解码的工具库.它和XML或者JSON差不多,也就是把某种数据结构的信息,以某种格式(XML,JSON)保存起来,protobuf与XML和JSON不同在于,protobuf是基于二进制的.主要用于数据存储.传输协议格式等场合.那既然有了XML等工具,为什么还要开发protobuf呢?主要是因为性能,包括时间开销和空间开销: 1.时间开销:XM