mac 安装protobuf

191 min read

使用brew安装protobuf

使用brew命令进行protobuf安装默认安装最新的版本

brew install protobuf

安装指定版本

brew reinstall [email protected]

简单使用

// 指定版本
syntax ="proto3";

//指定包名
package pb;

enum week {
    Monday =0; // 必须从0开始
    Thuesday =1;
}
message Student{
    string name =1; // 可以不用1开始,但是不能重复
    int32 age =2;
    Person p =3;
    repeated int32 score =4; // 数组or 切片
    week w=5;
    
    // 联合体
    oneof data {
        string teacher = 6;
        string class = 7;
    }
}

message Person{
    int32 weight =1;
}

编译脚本

protoc --go_out=. *.proto

生成 hello.pb.go

// Code generated by protoc-gen-go. DO NOT EDIT.
// source: hello.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.ProtoPackageIsVersion3 // please upgrade the proto package

type Week int32

const (
	Week_Monday   Week = 0
	Week_Thuesday Week = 1
)

var Week_name = map[int32]string{
	0: "Monday",
	1: "Thuesday",
}

var Week_value = map[string]int32{
	"Monday":   0,
	"Thuesday": 1,
}

func (x Week) String() string {
	return proto.EnumName(Week_name, int32(x))
}

func (Week) EnumDescriptor() ([]byte, []int) {
	return fileDescriptor_61ef911816e0a8ce, []int{0}
}

type Student 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"`
	P     *Person `protobuf:"bytes,3,opt,name=p,proto3" json:"p,omitempty"`
	Score []int32 `protobuf:"varint,4,rep,packed,name=score,proto3" json:"score,omitempty"`
	W     Week    `protobuf:"varint,5,opt,name=w,proto3,enum=pb.Week" json:"w,omitempty"`
	// 联合体
	//
	// Types that are valid to be assigned to Data:
	//	*Student_Teacher
	//	*Student_Class
	Data                 isStudent_Data `protobuf_oneof:"data"`
	XXX_NoUnkeyedLiteral struct{}       `json:"-"`
	XXX_unrecognized     []byte         `json:"-"`
	XXX_sizecache        int32          `json:"-"`
}

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

func (m *Student) XXX_Unmarshal(b []byte) error {
	return xxx_messageInfo_Student.Unmarshal(m, b)
}
func (m *Student) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
	return xxx_messageInfo_Student.Marshal(b, m, deterministic)
}
func (m *Student) XXX_Merge(src proto.Message) {
	xxx_messageInfo_Student.Merge(m, src)
}
func (m *Student) XXX_Size() int {
	return xxx_messageInfo_Student.Size(m)
}
func (m *Student) XXX_DiscardUnknown() {
	xxx_messageInfo_Student.DiscardUnknown(m)
}

var xxx_messageInfo_Student proto.InternalMessageInfo

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

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

func (m *Student) GetP() *Person {
	if m != nil {
		return m.P
	}
	return nil
}

func (m *Student) GetScore() []int32 {
	if m != nil {
		return m.Score
	}
	return nil
}

func (m *Student) GetW() Week {
	if m != nil {
		return m.W
	}
	return Week_Monday
}

type isStudent_Data interface {
	isStudent_Data()
}

type Student_Teacher struct {
	Teacher string `protobuf:"bytes,6,opt,name=teacher,proto3,oneof"`
}

type Student_Class struct {
	Class string `protobuf:"bytes,7,opt,name=class,proto3,oneof"`
}

func (*Student_Teacher) isStudent_Data() {}

func (*Student_Class) isStudent_Data() {}

func (m *Student) GetData() isStudent_Data {
	if m != nil {
		return m.Data
	}
	return nil
}

func (m *Student) GetTeacher() string {
	if x, ok := m.GetData().(*Student_Teacher); ok {
		return x.Teacher
	}
	return ""
}

func (m *Student) GetClass() string {
	if x, ok := m.GetData().(*Student_Class); ok {
		return x.Class
	}
	return ""
}

// XXX_OneofWrappers is for the internal use of the proto package.
func (*Student) XXX_OneofWrappers() []interface{} {
	return []interface{}{
		(*Student_Teacher)(nil),
		(*Student_Class)(nil),
	}
}

type Person struct {
	Weight               int32    `protobuf:"varint,1,opt,name=weight,proto3" json:"weight,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_61ef911816e0a8ce, []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) GetWeight() int32 {
	if m != nil {
		return m.Weight
	}
	return 0
}

func init() {
	proto.RegisterEnum("pb.Week", Week_name, Week_value)
	proto.RegisterType((*Student)(nil), "pb.Student")
	proto.RegisterType((*Person)(nil), "pb.Person")
}

func init() { proto.RegisterFile("hello.proto", fileDescriptor_61ef911816e0a8ce) }

var fileDescriptor_61ef911816e0a8ce = []byte{
	// 234 bytes of a gzipped FileDescriptorProto
	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x34, 0x90, 0xc1, 0x4a, 0xc4, 0x30,
	0x10, 0x86, 0x77, 0xb6, 0x4d, 0xba, 0xce, 0x8a, 0x2c, 0x83, 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dc, 0x7d, 0xf4, 0x17, 0x0e, 0x89, 0xa0, 0x91, 0x4b, 0x09, 0x4f, 0x7f, 0x01, 0x00, 0x00,
	0xff, 0xff, 0x11, 0xf6, 0x5e, 0xb5, 0x13, 0x01, 0x00, 0x00,
}

添加rpc服务

service 服务名{ 
  rpc 函数名(参数: 消息体) returns (返回值:消息)
}
service hello {
  rpc helloTest(Person) returns (Student);
}
  • 默认 proto编译不生成服务
  • gRPC生成服务 protoc --go_out=plugins=grpc:. *.proto