reflect

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: BSD-3-Clause Imports: 2 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 uintptr
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

func (Kind) String

func (k Kind) String() string

type MapIter added in v0.5.0

type MapIter struct {
}

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 SliceHeader

type SliceHeader struct {
	Data uintptr
	Len  uintptr
	Cap  uintptr
}

type StringHeader

type StringHeader struct {
	Data uintptr
	Len  uintptr
}

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
	Anonymous bool
	Offset    uintptr
	Index     []int // index sequence for Type.FieldByIndex
}

A StructField describes a single field in a struct.

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

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

	// FieldByName returns the struct field with the given name
	// and a boolean indicating if the field was found.
	FieldByName(name string) (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
}

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

func SliceOf(t Type) Type

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

func (v Value) CanAddr() bool

func (Value) CanInterface

func (v Value) CanInterface() bool

func (Value) CanSet

func (v Value) CanSet() bool

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

func (v Value) Float() float64

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

func (Value) OverflowInt added in v0.14.0

func (v Value) OverflowInt(x int64) bool

func (Value) OverflowUint added in v0.14.0

func (v Value) OverflowUint(x uint64) bool

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

type ValueError

type ValueError struct {
	Method string
	Kind   Kind
}

func (*ValueError) Error

func (e *ValueError) Error() string

Jump to

Keyboard shortcuts

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