315-protobuf入门二








protobuf入门二







protobuf编译

基本编译
可以通过定义好的.proto文件来生成go,java,python等语言的代码
需要基于.proto文件运行protocolbuffer编译器protoc

可以这样调用protocol编译器
protoc --proto_path=IMPORT_PATH --go_out=DST_DIR path/to/file.proto

我们可以直接在目录下这样
protoc --go_out=./ *.proto

这里的命令的意思就是
编译当前目录的所有.proto文件





我们现在目录下有一个
person.proto
然后
protoc --go_out=./ *.proto
然后我们发现生成了一个
person.pb.go







那么我们打开这个person.pb.go来看一下生成的go语言代码

// Code generated by protoc-gen-go. DO NOT EDIT.
// source: person.proto

package pb

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.ProtoPackageIsVersion2 // please upgrade the proto package

type Person struct {
	Name                 string   `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	Age                  int32    `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`
	Emails               []string `protobuf:"bytes,3,rep,name=emails,proto3" json:"emails,omitempty"`
	Phones               []string `protobuf:"bytes,4,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_4c9e10cf24b1156d, []int{0}
}

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) GetName() string {
	if m != nil {
		return m.Name
	}
	return ""
}

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

func (m *Person) GetEmails() []string {
	if m != nil {
		return m.Emails
	}
	return nil
}

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

func init() {
	proto.RegisterType((*Person)(nil), "pb.Person")
}

func init() { proto.RegisterFile("person.proto", fileDescriptor_4c9e10cf24b1156d) }

var fileDescriptor_4c9e10cf24b1156d = []byte{
	// 119 bytes of a gzipped FileDescriptorProto
	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x48, 0x2d, 0x2a,
	0xce, 0xcf, 0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x2a, 0x48, 0x52, 0x8a, 0xe3, 0x62,
	0x0b, 0x00, 0x8b, 0x09, 0x09, 0x71, 0xb1, 0xe4, 0x25, 0xe6, 0xa6, 0x4a, 0x30, 0x2a, 0x30, 0x6a,
	0x70, 0x06, 0x81, 0xd9, 0x42, 0x02, 0x5c, 0xcc, 0x89, 0xe9, 0xa9, 0x12, 0x4c, 0x0a, 0x8c, 0x1a,
	0xac, 0x41, 0x20, 0xa6, 0x90, 0x18, 0x17, 0x5b, 0x6a, 0x6e, 0x62, 0x66, 0x4e, 0xb1, 0x04, 0xb3,
	0x02, 0xb3, 0x06, 0x67, 0x10, 0x94, 0x07, 0x12, 0x2f, 0xc8, 0xc8, 0xcf, 0x4b, 0x2d, 0x96, 0x60,
	0x81, 0x88, 0x43, 0x78, 0x49, 0x6c, 0x60, 0xab, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xb1,
	0x41, 0xb7, 0xf1, 0x7a, 0x00, 0x00, 0x00,
}












 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值