reflect

package
v0.37.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RecvDir = reflectlite.RecvDir
	SendDir = reflectlite.SendDir
	BothDir = reflectlite.BothDir
)

Variables

This section is empty.

Functions

func Copy added in v0.14.0

func Copy(dst, src Value) int

Copy copies the contents of src into dst until either dst has been filled or src has been exhausted.

func DeepEqual added in v0.14.0

func DeepEqual(x, y interface{}) bool

func Swapper

func Swapper(slice interface{}) func(i, j int)

Types

type ChanDir added in v0.23.0

type ChanDir = reflectlite.ChanDir

type MapIter added in v0.5.0

type MapIter reflectlite.MapIter

func (*MapIter) Key added in v0.5.0

func (it *MapIter) Key() Value

func (*MapIter) Next added in v0.5.0

func (it *MapIter) Next() bool

func (*MapIter) Reset added in v0.37.0

func (iter *MapIter) Reset(v Value)

func (*MapIter) Value added in v0.5.0

func (it *MapIter) Value() Value

type Method added in v0.23.0

type Method struct {
	// Name is the method name.
	Name string

	// PkgPath is the package path that qualifies a lower case (unexported)
	// method name. It is empty for upper case (exported) method names.
	// The combination of PkgPath and Name uniquely identifies a method
	// in a method set.
	// See https://golang.org/ref/spec#Uniqueness_of_identifiers
	PkgPath string

	Type  Type  // method type
	Func  Value // func with receiver as first argument
	Index int   // index for Type.Method
}

Method represents a single method.

type SelectCase added in v0.28.0

type SelectCase struct {
	Dir  SelectDir // direction of case
	Chan Value     // channel to use (for send or receive)
	Send Value     // value to send (for send)
}

type SelectDir added in v0.28.0

type SelectDir int
const (
	SelectSend    SelectDir // case Chan <- Send
	SelectRecv              // case <-Chan:
	SelectDefault           // default
)

type SliceHeader deprecated

type SliceHeader struct {
	Data uintptr
	Len  intw
	Cap  intw
}

Deprecated: Use unsafe.Slice or unsafe.SliceData instead.

type StringHeader deprecated

type StringHeader struct {
	Data uintptr
	Len  intw
}

Deprecated: Use unsafe.String or unsafe.StringData instead.

type StructField

type StructField struct {
	// Name indicates the field name.
	Name string

	// PkgPath is the package path where the struct containing this field is
	// declared for unexported fields, or the empty string for exported fields.
	PkgPath string

	Type      Type
	Tag       StructTag // field tag string
	Offset    uintptr
	Index     []int // index sequence for Type.FieldByIndex
	Anonymous bool
}

A StructField describes a single field in a struct. This must be kept in sync with reflectlite.StructField.

func VisibleFields added in v0.28.0

func VisibleFields(t Type) []StructField

func (StructField) IsExported added in v0.20.0

func (f StructField) IsExported() bool

IsExported reports whether the field is exported.

type StructTag added in v0.14.0

type StructTag = reflectlite.StructTag

type Type

type Type interface {

	// Align returns the alignment in bytes of a value of
	// this type when allocated in memory.
	Align() int

	// FieldAlign returns the alignment in bytes of a value of
	// this type when used as a field in a struct.
	FieldAlign() int

	// Method returns the i'th method in the type's method set.
	// It panics if i is not in the range [0, NumMethod()).
	//
	// For a non-interface type T or *T, the returned Method's Type and Func
	// fields describe a function whose first argument is the receiver.
	//
	// For an interface type, the returned Method's Type field gives the
	// method signature, without a receiver, and the Func field is nil.
	//
	// Only exported methods are accessible and they are sorted in
	// lexicographic order.
	Method(int) Method

	// MethodByName returns the method with that name in the type's
	// method set and a boolean indicating if the method was found.
	//
	// For a non-interface type T or *T, the returned Method's Type and Func
	// fields describe a function whose first argument is the receiver.
	//
	// For an interface type, the returned Method's Type field gives the
	// method signature, without a receiver, and the Func field is nil.
	MethodByName(string) (Method, bool)

	// NumMethod returns the number of exported methods in the type's method set.
	NumMethod() int

	// Name returns the type's name within its package for a defined type.
	// For other (non-defined) types it returns the empty string.
	Name() string

	// PkgPath returns a defined type's package path, that is, the import path
	// that uniquely identifies the package, such as "encoding/base64".
	// If the type was predeclared (string, error) or not defined (*T, struct{},
	// []int, or A where A is an alias for a non-defined type), the package path
	// will be the empty string.
	PkgPath() string

	// Size returns the number of bytes needed to store
	// a value of the given type; it is analogous to unsafe.Sizeof.
	Size() uintptr

	// String returns a string representation of the type.
	// The string representation may use shortened package names
	// (e.g., base64 instead of "encoding/base64") and is not
	// guaranteed to be unique among types. To test for type identity,
	// compare the Types directly.
	String() string

	// Kind returns the specific kind of this type.
	Kind() Kind

	// Implements reports whether the type implements the interface type u.
	Implements(u Type) bool

	// AssignableTo reports whether a value of the type is assignable to type u.
	AssignableTo(u Type) bool

	// ConvertibleTo reports whether a value of the type is convertible to type u.
	ConvertibleTo(u Type) bool

	// Comparable reports whether values of this type are comparable.
	Comparable() bool

	// Bits returns the size of the type in bits.
	// It panics if the type's Kind is not one of the
	// sized or unsized Int, Uint, Float, or Complex kinds.
	Bits() int

	// ChanDir returns a channel type's direction.
	// It panics if the type's Kind is not Chan.
	ChanDir() ChanDir

	// IsVariadic reports whether a function type's final input parameter
	// is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's
	// implicit actual type []T.
	//
	// For concreteness, if t represents func(x int, y ... float64), then
	//
	//	t.NumIn() == 2
	//	t.In(0) is the reflect.Type for "int"
	//	t.In(1) is the reflect.Type for "[]float64"
	//	t.IsVariadic() == true
	//
	// IsVariadic panics if the type's Kind is not Func.
	IsVariadic() bool

	// Elem returns a type's element type.
	// It panics if the type's Kind is not Array, Chan, Map, Pointer, or Slice.
	Elem() Type

	// Field returns a struct type's i'th field.
	// It panics if the type's Kind is not Struct.
	// It panics if i is not in the range [0, NumField()).
	Field(i int) StructField

	// FieldByIndex returns the nested field corresponding
	// to the index sequence. It is equivalent to calling Field
	// successively for each index i.
	// It panics if the type's Kind is not Struct.
	FieldByIndex(index []int) StructField

	// FieldByName returns the struct field with the given name
	// and a boolean indicating if the field was found.
	FieldByName(name string) (StructField, bool)

	// FieldByNameFunc returns the struct field with a name
	// that satisfies the match function and a boolean indicating if
	// the field was found.
	//
	// FieldByNameFunc considers the fields in the struct itself
	// and then the fields in any embedded structs, in breadth first order,
	// stopping at the shallowest nesting depth containing one or more
	// fields satisfying the match function. If multiple fields at that depth
	// satisfy the match function, they cancel each other
	// and FieldByNameFunc returns no match.
	// This behavior mirrors Go's handling of name lookup in
	// structs containing embedded fields.
	FieldByNameFunc(match func(string) bool) (StructField, bool)

	// In returns the type of a function type's i'th input parameter.
	// It panics if the type's Kind is not Func.
	// It panics if i is not in the range [0, NumIn()).
	In(i int) Type

	// Key returns a map type's key type.
	// It panics if the type's Kind is not Map.
	Key() Type

	// Len returns an array type's length.
	// It panics if the type's Kind is not Array.
	Len() int

	// NumField returns a struct type's field count.
	// It panics if the type's Kind is not Struct.
	NumField() int

	// NumIn returns a function type's input parameter count.
	// It panics if the type's Kind is not Func.
	NumIn() int

	// NumOut returns a function type's output parameter count.
	// It panics if the type's Kind is not Func.
	NumOut() int

	// Out returns the type of a function type's i'th output parameter.
	// It panics if the type's Kind is not Func.
	// It panics if i is not in the range [0, NumOut()).
	Out(i int) Type

	// OverflowComplex reports whether the complex128 x cannot be represented by type t.
	// It panics if t's Kind is not Complex64 or Complex128.
	OverflowComplex(x complex128) bool

	// OverflowFloat reports whether the float64 x cannot be represented by type t.
	// It panics if t's Kind is not Float32 or Float64.
	OverflowFloat(x float64) bool

	// OverflowInt reports whether the int64 x cannot be represented by type t.
	// It panics if t's Kind is not Int, Int8, Int16, Int32, or Int64.
	OverflowInt(x int64) bool

	// OverflowUint reports whether the uint64 x cannot be represented by type t.
	// It panics if t's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
	OverflowUint(x uint64) bool

	// CanSeq reports whether a [Value] with this type can be iterated over using [Value.Seq].
	CanSeq() bool

	// CanSeq2 reports whether a [Value] with this type can be iterated over using [Value.Seq2].
	CanSeq2() bool
}

Type is the representation of a Go type.

Not all methods apply to all kinds of types. Restrictions, if any, are noted in the documentation for each method. Use the Kind method to find out the kind of type before calling kind-specific methods. Calling a method inappropriate to the kind of type causes a run-time panic.

Type values are comparable, such as with the == operator, so they can be used as map keys. Two Type values are equal if they represent identical types.

func PointerTo added in v0.23.0

func PointerTo(t Type) Type

func PtrTo added in v0.17.0

func PtrTo(t Type) Type

func TypeFor added in v0.31.0

func TypeFor[T any]() Type

func TypeOf

func TypeOf(i interface{}) Type

type Value

type Value struct {
	reflectlite.Value
}

func Append added in v0.14.0

func Append(v Value, x ...Value) Value

Append appends the values x to a slice s and returns the resulting slice. As in Go, each x's value must be assignable to the slice's element type.

func AppendSlice added in v0.19.0

func AppendSlice(s, t Value) Value

AppendSlice appends a slice t to a slice s and returns the resulting slice. The slices s and t must have the same element type.

func Indirect

func Indirect(v Value) Value

func MakeFunc added in v0.19.0

func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value

func MakeMap added in v0.14.0

func MakeMap(typ Type) Value

MakeMap creates a new map with the specified type.

func MakeMapWithSize added in v0.28.0

func MakeMapWithSize(typ Type, n int) Value

MakeMapWithSize creates a new map with the specified type and initial space for approximately n elements.

func MakeSlice

func MakeSlice(typ Type, len, cap int) Value

func New added in v0.7.0

func New(typ Type) Value

New is the reflect equivalent of the new(T) keyword, returning a pointer to a new value of the given type.

func NewAt added in v0.23.0

func NewAt(typ Type, p unsafe.Pointer) Value

func Select added in v0.28.0

func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool)

func ValueOf

func ValueOf(i interface{}) Value

func Zero added in v0.7.0

func Zero(typ Type) Value

func (Value) Addr

func (v Value) Addr() Value

func (Value) Call added in v0.19.0

func (v Value) Call(in []Value) []Value

func (Value) CallSlice added in v0.31.0

func (v Value) CallSlice(in []Value) []Value

func (Value) CanConvert added in v0.28.0

func (v Value) CanConvert(t Type) bool

func (Value) Close added in v0.28.0

func (v Value) Close()

func (Value) Convert added in v0.14.0

func (v Value) Convert(t Type) Value

func (Value) Elem

func (v Value) Elem() Value

func (Value) Equal added in v0.36.0

func (v Value) Equal(u Value) bool

func (Value) Field

func (v Value) Field(i int) Value

Field returns the value of the i'th field of this struct.

func (Value) FieldByIndex added in v0.14.0

func (v Value) FieldByIndex(index []int) Value

FieldByIndex returns the nested field corresponding to index.

func (Value) FieldByIndexErr added in v0.23.0

func (v Value) FieldByIndexErr(index []int) (Value, error)

FieldByIndexErr returns the nested field corresponding to index.

func (Value) FieldByName added in v0.14.0

func (v Value) FieldByName(name string) Value

func (Value) FieldByNameFunc added in v0.29.0

func (v Value) FieldByNameFunc(match func(string) bool) Value

func (Value) Index

func (v Value) Index(i int) Value

func (Value) MapIndex

func (v Value) MapIndex(key Value) Value

func (Value) MapKeys

func (v Value) MapKeys() []Value

func (Value) MapRange added in v0.5.0

func (v Value) MapRange() *MapIter

func (Value) Method added in v0.28.0

func (v Value) Method(i int) Value

func (Value) MethodByName added in v0.23.0

func (v Value) MethodByName(name string) Value

func (Value) Recv added in v0.23.0

func (v Value) Recv() (x Value, ok bool)

func (Value) Send added in v0.28.0

func (v Value) Send(x Value)

func (Value) Seq added in v0.37.0

func (v Value) Seq() iter.Seq[Value]

Seq returns an iter.Seq[Value] that loops over the elements of v. If v's kind is Func, it must be a function that has no results and that takes a single argument of type func(T) bool for some type T. If v's kind is Pointer, the pointer element type must have kind Array. Otherwise v's kind must be Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Array, Chan, Map, Slice, or String.

func (Value) Seq2 added in v0.37.0

func (v Value) Seq2() iter.Seq2[Value, Value]

Seq2 returns an iter.Seq2[Value, Value] that loops over the elements of v. If v's kind is Func, it must be a function that has no results and that takes a single argument of type func(K, V) bool for some type K, V. If v's kind is Pointer, the pointer element type must have kind Array. Otherwise v's kind must be Array, Map, Slice, or String.

func (Value) Set

func (v Value) Set(x Value)

func (Value) SetIterKey added in v0.37.0

func (v Value) SetIterKey(iter *MapIter)

func (Value) SetIterValue added in v0.37.0

func (v Value) SetIterValue(iter *MapIter)

func (Value) SetMapIndex added in v0.14.0

func (v Value) SetMapIndex(key, elem Value)

func (Value) Slice

func (v Value) Slice(i, j int) Value

func (Value) Slice3 added in v0.23.0

func (v Value) Slice3(i, j, k int) Value

func (Value) Type

func (v Value) Type() Type

type ValueError

type ValueError = reflectlite.ValueError

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL