The runtime packages provide low-level interfaces and implementation details used by generated protocol buffer code. These packages define the contract between generated code and the runtime library, including fast-path optimizations, message implementation infrastructure, and lazy decoding control.
WARNING: These packages should only be imported by generated message implementations. The compatibility agreement covers only functionality needed to keep existing generated messages operational.
import (
"google.golang.org/protobuf/runtime/protoiface"
"google.golang.org/protobuf/runtime/protoimpl"
"google.golang.org/protobuf/runtime/protolazy"
"google.golang.org/protobuf/reflect/protoreflect"
)The v1 proto.Message interface for backward compatibility.
// MessageV1 is the v1 proto.Message interface
type MessageV1 interface {
Reset()
String() string
ProtoMessage()
}Optional fast-path implementations of various message operations.
// Methods is a set of optional fast-path implementations of various operations
type Methods struct {
pragma.NoUnkeyedLiterals
// Flags indicate support for optional features
Flags SupportFlags
// Size returns the size in bytes of the wire-format encoding of a message
// Marshal must be provided if a custom Size is provided
Size func(SizeInput) SizeOutput
// Marshal formats a message in the wire-format encoding to the provided buffer
// Size should be provided if a custom Marshal is provided
// It must not return an error for a partial message
Marshal func(MarshalInput) (MarshalOutput, error)
// Unmarshal parses the wire-format encoding and merges the result into a message
// It must not reset the target message or return an error for a partial message
Unmarshal func(UnmarshalInput) (UnmarshalOutput, error)
// Merge merges the contents of a source message into a destination message
Merge func(MergeInput) MergeOutput
// CheckInitialized returns an error if any required fields in the message are not set
CheckInitialized func(CheckInitializedInput) (CheckInitializedOutput, error)
// Equal compares two messages and returns EqualOutput.Equal == true if they are equal
Equal func(EqualInput) EqualOutput
}Indicate support for optional features in fast-path methods.
// SupportFlags indicate support for optional features
type SupportFlags uint64
const (
// SupportMarshalDeterministic reports whether MarshalOptions.Deterministic is supported
SupportMarshalDeterministic SupportFlags = 1 << iota
// SupportUnmarshalDiscardUnknown reports whether UnmarshalOptions.DiscardUnknown is supported
SupportUnmarshalDiscardUnknown
)Types for the Marshal fast-path method.
// MarshalInput is input to the Marshal method
type MarshalInput struct {
pragma.NoUnkeyedLiterals
Message protoreflect.Message
Buf []byte // output is appended to this buffer
Flags MarshalInputFlags
}
// MarshalInputFlags configure the marshaler
// Most flags correspond to fields in proto.MarshalOptions
type MarshalInputFlags uint8
const (
MarshalDeterministic MarshalInputFlags = 1 << iota
MarshalUseCachedSize
)
// MarshalOutput is output from the Marshal method
type MarshalOutput struct {
pragma.NoUnkeyedLiterals
Buf []byte // contains marshaled message
}Types for the Size fast-path method.
// SizeInput is input to the Size method
type SizeInput struct {
pragma.NoUnkeyedLiterals
Message protoreflect.Message
Flags MarshalInputFlags
}
// SizeOutput is output from the Size method
type SizeOutput struct {
pragma.NoUnkeyedLiterals
Size int
}Types for the Unmarshal fast-path method.
// UnmarshalInput is input to the Unmarshal method
type UnmarshalInput struct {
pragma.NoUnkeyedLiterals
Message protoreflect.Message
Buf []byte // input buffer
Flags UnmarshalInputFlags
Resolver interface {
FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
}
Depth int
}
// UnmarshalInputFlags configure the unmarshaler
// Most flags correspond to fields in proto.UnmarshalOptions
type UnmarshalInputFlags uint8
const (
UnmarshalDiscardUnknown UnmarshalInputFlags = 1 << iota
// UnmarshalAliasBuffer permits unmarshal operations to alias the input buffer
// The unmarshaller must not modify the contents of the buffer
UnmarshalAliasBuffer
// UnmarshalValidated indicates that validation has already been
// performed on the input buffer
UnmarshalValidated
// UnmarshalCheckRequired is set if this unmarshal operation ultimately will care if required fields are
// initialized
UnmarshalCheckRequired
// UnmarshalNoLazyDecoding is set if this unmarshal operation should not use
// lazy decoding, even when otherwise available
UnmarshalNoLazyDecoding
)
// UnmarshalOutput is output from the Unmarshal method
type UnmarshalOutput struct {
pragma.NoUnkeyedLiterals
Flags UnmarshalOutputFlags
}
// UnmarshalOutputFlags are output from the Unmarshal method
type UnmarshalOutputFlags uint8
const (
// UnmarshalInitialized may be set on return if all required fields are known to be set
// If unset, then it does not necessarily indicate that the message is uninitialized,
// only that its status could not be confirmed
UnmarshalInitialized UnmarshalOutputFlags = 1 << iota
)Types for the Merge fast-path method.
// MergeInput is input to the Merge method
type MergeInput struct {
pragma.NoUnkeyedLiterals
Source protoreflect.Message
Destination protoreflect.Message
}
// MergeOutput is output from the Merge method
type MergeOutput struct {
pragma.NoUnkeyedLiterals
Flags MergeOutputFlags
}
// MergeOutputFlags are output from the Merge method
type MergeOutputFlags uint8
const (
// MergeComplete reports whether the merge was performed
// If unset, the merger must have made no changes to the destination
MergeComplete MergeOutputFlags = 1 << iota
)Types for the CheckInitialized fast-path method.
// CheckInitializedInput is input to the CheckInitialized method
type CheckInitializedInput struct {
pragma.NoUnkeyedLiterals
Message protoreflect.Message
}
// CheckInitializedOutput is output from the CheckInitialized method
type CheckInitializedOutput struct {
pragma.NoUnkeyedLiterals
}Types for the Equal fast-path method.
// EqualInput is input to the Equal method
type EqualInput struct {
pragma.NoUnkeyedLiterals
MessageA protoreflect.Message
MessageB protoreflect.Message
}
// EqualOutput is output from the Equal method
type EqualOutput struct {
pragma.NoUnkeyedLiterals
Equal bool
}// ExtensionRangeV1 is the legacy extension range type
type ExtensionRangeV1 struct {
Start, End int32 // both inclusive
}Ensure compatibility between generated code and the runtime library.
// EnforceVersion is used by code generated by protoc-gen-go to statically
// enforce minimum and maximum versions of this package
// A compilation failure implies either that:
// - the runtime package is too old and needs to be updated OR
// - the generated code is too old and needs to be regenerated
type EnforceVersion uint
const (
// MaxVersion is the maximum supported version for generated .pb.go files
// It is always the current version of the module
MaxVersion = version.Minor
// GenVersion is the runtime version required by generated .pb.go files
// This is incremented when generated code relies on new functionality
// in the runtime
GenVersion = 20
// MinVersion is the minimum supported version for generated .pb.go files
// This is incremented when the runtime drops support for old code
MinVersion = 0
)Usage in generated code:
const (
// Verify that this generated code is sufficiently up-to-date
_ = protoimpl.EnforceVersion(genVersion - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - genVersion)
)Types embedded in or used by generated messages.
// MessageInfo provides infrastructure for generated message types
type MessageInfo impl.MessageInfo
// MessageState is the state for message implementations
type MessageState impl.MessageState
// EnumInfo provides infrastructure for generated enum types
type EnumInfo impl.EnumInfo
// ExtensionInfo provides infrastructure for extension fields
type ExtensionInfo impl.ExtensionInfo
// ExtensionFields stores extension fields
type ExtensionFields impl.ExtensionFields
// ExtensionFieldV1 is the legacy extension field type
type ExtensionFieldV1 impl.ExtensionField
// UnknownFields stores unknown fields
type UnknownFields impl.UnknownFields
// WeakFields stores weak fields (deprecated)
type WeakFields impl.WeakFields
// SizeCache caches the encoded size of a message
type SizeCache impl.SizeCache
// Pointer is a pointer to a message
type Pointer impl.Pointer
// LazyUnmarshalInfo stores lazy unmarshal information
type LazyUnmarshalInfo *protolazy.XXX_lazyUnmarshalInfoTypes used by generated code in init functions.
// DescBuilder builds descriptors
type DescBuilder filedesc.Builder
// TypeBuilder builds types
type TypeBuilder filetype.BuilderExported functionality for generated code.
// X contains exported functionality for generated code
var X impl.Export// UnsafeEnabled specifies whether package unsafe can be used
const UnsafeEnabled = impl.UnsafeEnabled// RaceDetectHookData provides hooks for race detection
type RaceDetectHookData impl.RaceDetectHookDataControl lazy unmarshaling of opaque messages at runtime.
// Disable disables lazy unmarshaling of opaque messages
//
// Messages which are still on the OPEN or HYBRID API level are never lazily
// unmarshalled.
//
// Fields must be annotated with [lazy = true] in their .proto file to become
// eligible for lazy unmarshaling.
//
// Returns a function that can be called to re-enable lazy decoding.
func Disable() (reenable func())The following logic determines whether lazy decoding is enabled:
GOPROTODEBUG=nolazy is set.protolazy.Disable() turns off lazy decoding.proto.UnmarshalOptions's NoLazyDecoding turns off lazy decoding for that Unmarshal operation only.Usage example:
import "google.golang.org/protobuf/runtime/protolazy"
// Temporarily disable lazy decoding
reenable := protolazy.Disable()
defer reenable()
// Unmarshal operations here will not use lazy decoding
data, _ := proto.Marshal(msg)
msg2 := &pb.MyMessage{}
proto.Unmarshal(data, msg2)The runtime packages are internal implementation details of the protobuf-go module. They provide:
These packages should only be used by code generated by protoc-gen-go. Direct usage in user code is not supported and may break without notice.