reflect

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const Ptr = Pointer

Ptr is the old name for the Pointer kind.

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

DeepEqual reports whether x and y are “deeply equal”, defined as follows. Two values of identical type are deeply equal if one of the following cases applies. Values of distinct types are never deeply equal.

Array values are deeply equal when their corresponding elements are deeply equal.

Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.

Func values are deeply equal if both are nil; otherwise they are not deeply equal.

Interface values are deeply equal if they hold deeply equal concrete values.

Map values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they are the same map object or their corresponding keys (matched using Go equality) map to deeply equal values.

Pointer values are deeply equal if they are equal using Go's == operator or if they point to deeply equal values.

Slice values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) are not deeply equal.

Other values - numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.

In general DeepEqual is a recursive relaxation of Go's == operator. However, this idea is impossible to implement without some inconsistency. Specifically, it is possible for a value to be unequal to itself, either because it is of func type (uncomparable in general) or because it is a floating-point NaN value (not equal to itself in floating-point comparison), or because it is an array, struct, or interface containing such a value. On the other hand, pointer values are always equal to themselves, even if they point at or contain such problematic values, because they compare equal using Go's == operator, and that is a sufficient condition to be deeply equal, regardless of content. DeepEqual has been defined so that the same short-cut applies to slices and maps: if x and y are the same slice or the same map, they are deeply equal regardless of content.

As DeepEqual traverses the data values it may find a cycle. The second and subsequent times that DeepEqual compares two pointer values that have been compared before, it treats the values as equal rather than examining the values to which they point. This ensures that DeepEqual terminates.

func Swapper

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

Types

type ChanDir added in v0.23.0

type ChanDir int

ChanDir represents a channel type's direction.

const (
	RecvDir ChanDir             = 1 << iota // <-chan
	SendDir                                 // chan<-
	BothDir = RecvDir | SendDir             // chan
)

type Kind

type Kind uint8
const (
	Invalid Kind = iota
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	String
	UnsafePointer
	Chan
	Interface
	Pointer
	Slice
	Array
	Func
	Map
	Struct
)

Copied from reflect/type.go https://golang.org/src/reflect/type.go?s=8302:8316#L217 These constants must match basicTypes and the typeKind* constants in compiler/interface.go

func (Kind) String

func (k Kind) String() string

type MapIter added in v0.5.0

type MapIter struct {
	// contains filtered or unexported fields
}

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) 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

type SliceHeader struct {
	Data uintptr
	Len  intw
	Cap  intw
}

type StringHeader

type StringHeader struct {
	Data uintptr
	Len  intw
}

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.

func VisibleFields added in v0.28.0

func VisibleFields(t Type) []StructField

VisibleFields returns all the visible fields in t, which must be a struct type. A field is defined as visible if it's accessible directly with a FieldByName call. The returned fields include fields inside anonymous struct members and unexported fields. They follow the same order found in the struct, with anonymous fields followed immediately by their promoted fields.

For each element e of the returned slice, the corresponding field can be retrieved from a value v of type t by calling v.FieldByIndex(e.Index).

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 string

A StructTag is the tag string in a struct field.

func (StructTag) Get added in v0.14.0

func (tag StructTag) Get(key string) string

Get returns the value associated with key in the tag string.

func (StructTag) Lookup added in v0.14.0

func (tag StructTag) Lookup(key string) (value string, ok bool)

Lookup returns the value associated with key in the tag string.

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
}

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 ArrayOf added in v0.28.0

func ArrayOf(n int, t Type) Type

func FuncOf added in v0.31.0

func FuncOf(in, out []Type, variadic bool) Type

func MapOf added in v0.28.0

func MapOf(key, value Type) Type

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 SliceOf added in v0.23.0

func SliceOf(t Type) Type

func StructOf added in v0.28.0

func StructOf([]StructField) Type

func TypeFor added in v0.31.0

func TypeFor[T any]() Type

TypeFor returns the Type that represents the type argument T.

func TypeOf

func TypeOf(i interface{}) Type

type TypeError added in v0.8.0

type TypeError struct {
	Method string
}

TypeError is the error that is used in a panic when invoking a method on a type that is not applicable to that type.

func (*TypeError) Error added in v0.8.0

func (e *TypeError) Error() string

type Value

type Value struct {
	// contains filtered or unexported fields
}

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) Bool

func (v Value) Bool() bool

func (Value) Bytes

func (v Value) Bytes() []byte

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) CanAddr

func (v Value) CanAddr() bool

func (Value) CanComplex added in v0.28.0

func (v Value) CanComplex() bool

CanComplex reports whether Complex can be used without panicking.

func (Value) CanConvert added in v0.28.0

func (v Value) CanConvert(t Type) bool

func (Value) CanFloat added in v0.28.0

func (v Value) CanFloat() bool

CanFloat reports whether Float can be used without panicking.

func (Value) CanInt added in v0.28.0

func (v Value) CanInt() bool

CanInt reports whether Uint can be used without panicking.

func (Value) CanInterface

func (v Value) CanInterface() bool

func (Value) CanSet

func (v Value) CanSet() bool

func (Value) CanUint added in v0.28.0

func (v Value) CanUint() bool

CanUint reports whether Uint can be used without panicking.

func (Value) Cap

func (v Value) Cap() int

Cap returns the capacity of this value for arrays, channels and slices. For other types, it panics.

func (Value) Clear added in v0.35.0

func (v Value) Clear()

Clear clears the contents of a map or zeros the contents of a slice

It panics if v's Kind is not Map or Slice.

func (Value) Close added in v0.28.0

func (v Value) Close()

func (Value) Comparable added in v0.28.0

func (v Value) Comparable() bool

func (Value) Complex

func (v Value) Complex() complex128

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) 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) Float

func (v Value) Float() float64

func (Value) Float32 added in v0.28.0

func (v Value) Float32() float32

func (Value) Grow added in v0.29.0

func (v Value) Grow(n int)

Grow increases the slice's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the slice without another allocation.

It panics if v's Kind is not a Slice or if n is negative or too large to allocate the memory.

func (Value) Index

func (v Value) Index(i int) Value

func (Value) Int

func (v Value) Int() int64

func (Value) Interface

func (v Value) Interface() interface{}

func (Value) IsNil

func (v Value) IsNil() bool

IsNil returns whether the value is the nil value. It panics if the value Kind is not a channel, map, pointer, function, slice, or interface.

func (Value) IsValid

func (v Value) IsValid() bool

func (Value) IsZero added in v0.27.0

func (v Value) IsZero() bool

IsZero reports whether v is the zero value for its type. It panics if the argument is invalid.

func (Value) Kind

func (v Value) Kind() Kind

func (Value) Len

func (v Value) Len() int

Len returns the length of this value for slices, strings, arrays, channels, and maps. For other types, it panics.

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) NumField

func (v Value) NumField() int

NumField returns the number of fields of this struct. It panics for other value types.

func (Value) NumMethod added in v0.18.0

func (v Value) NumMethod() int

func (Value) OverflowFloat added in v0.18.0

func (v Value) OverflowFloat(x float64) bool

OverflowFloat reports whether the float64 x cannot be represented by v's type. It panics if v's Kind is not Float32 or Float64.

func (Value) OverflowInt added in v0.14.0

func (v Value) OverflowInt(x int64) bool

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

func (Value) OverflowUint added in v0.14.0

func (v Value) OverflowUint(x uint64) bool

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

func (Value) Pointer

func (v Value) Pointer() uintptr

Pointer returns the underlying pointer of the given value for the following types: chan, map, pointer, unsafe.Pointer, slice, func.

func (Value) RawType added in v0.18.0

func (v Value) RawType() *rawType

Internal function only, do not use.

RawType returns the raw, underlying type code. It is used in the runtime package and needs to be exported for the runtime package to access it.

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) Set

func (v Value) Set(x Value)

func (Value) SetBool

func (v Value) SetBool(x bool)

func (Value) SetBytes added in v0.18.0

func (v Value) SetBytes(x []byte)

func (Value) SetCap added in v0.18.0

func (v Value) SetCap(n int)

func (Value) SetComplex

func (v Value) SetComplex(x complex128)

func (Value) SetFloat

func (v Value) SetFloat(x float64)

func (Value) SetInt

func (v Value) SetInt(x int64)

func (Value) SetLen added in v0.18.0

func (v Value) SetLen(n int)

func (Value) SetMapIndex added in v0.14.0

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

func (Value) SetString

func (v Value) SetString(x string)

func (Value) SetUint

func (v Value) SetUint(x uint64)

func (Value) SetZero added in v0.29.0

func (v Value) SetZero()

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) String

func (v Value) String() string

func (Value) Type

func (v Value) Type() Type

func (Value) Uint

func (v Value) Uint() uint64

func (Value) UnsafeAddr added in v0.28.0

func (v Value) UnsafeAddr() uintptr

func (Value) UnsafePointer added in v0.24.0

func (v Value) UnsafePointer() unsafe.Pointer

UnsafePointer returns the underlying pointer of the given value for the following types: chan, map, pointer, unsafe.Pointer, slice, func.

type ValueError

type ValueError struct {
	Method string
	Kind   Kind
}

func (*ValueError) Error

func (e *ValueError) Error() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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