跳到主要内容

Protobuf Go

APIv2

  • v2 protoreflect.ProtoMessage
  • v1 protoiface.MessageV1
protoreflect
// 基于反射的主要接口
type ProtoMessage interface{ ProtoReflect() Message }

// v2
// 反射信息
type Message interface {
Descriptor() MessageDescriptor
Type() MessageType

New() Message
Interface() ProtoMessage
Range(f func(FieldDescriptor, Value) bool)
Has(FieldDescriptor) bool
Clear(FieldDescriptor)
Get(FieldDescriptor) Value
Set(FieldDescriptor, Value)
Mutable(FieldDescriptor) Value
NewField(FieldDescriptor) Value
WhichOneof(OneofDescriptor) FieldDescriptor
GetUnknown() RawFields
SetUnknown(RawFields)

IsValid() bool
ProtoMethods() *methods
}

// v1
type MessageV1 interface {
Reset()
String() string
ProtoMessage()
}

proto

  • golang proto v2 基于反射
  • github.com/golang/protobuf/proto
  • 反射
    • 全局注册 - google.golang.org/protobuf/reflect/protoregistry
      • GlobalFiles
      • GlobalTypes