Documentation ¶
Overview ¶
Package reflect implements run-time reflection, allowing a program to manipulate objects with arbitrary types. The typical use is to take a value with static type interface{} and extract its dynamic type information by calling TypeOf, which returns a Type.
A call to ValueOf returns a Value representing the run-time data. Zero takes a Type and returns a Value representing a zero value for that type.
Index ¶
- func Copy(dst, src Value) int
- func DeepEqual(a1, a2 interface{}) bool
- type ChanDir
- type Kind
- type Method
- type SliceHeader
- type StringHeader
- type StructField
- type Type
- type Value
- func Append(s Value, x ...Value) Value
- func AppendSlice(s, t Value) Value
- func Indirect(v Value) Value
- func MakeChan(typ Type, buffer int) Value
- func MakeMap(typ Type) Value
- func MakeSlice(typ Type, len, cap int) Value
- func New(typ Type) Value
- func ValueOf(i interface{}) Value
- func Zero(typ Type) Value
- func (v Value) Addr() Value
- func (v Value) Bool() bool
- func (v Value) Call(in []Value) []Value
- func (v Value) CallSlice(in []Value) []Value
- func (v Value) CanAddr() bool
- func (v Value) CanInterface() bool
- func (v Value) CanSet() bool
- func (v Value) Cap() int
- func (v Value) Close()
- func (v Value) Complex() complex128
- func (v Value) Elem() Value
- func (v Value) Field(i int) Value
- func (v Value) FieldByIndex(index []int) Value
- func (v Value) FieldByName(name string) Value
- func (v Value) FieldByNameFunc(match func(string) bool) Value
- func (v Value) Float() float64
- func (v Value) Index(i int) Value
- func (v Value) Int() int64
- func (v Value) Interface() interface{}
- func (v Value) InterfaceData() [2]uintptr
- func (v Value) IsNil() bool
- func (v Value) IsValid() bool
- func (v Value) Kind() Kind
- func (v Value) Len() int
- func (v Value) MapIndex(key Value) Value
- func (v Value) MapKeys() []Value
- func (v Value) Method(i int) Value
- func (v Value) NumField() int
- func (v Value) OverflowComplex(x complex128) bool
- func (v Value) OverflowFloat(x float64) bool
- func (v Value) OverflowInt(x int64) bool
- func (v Value) OverflowUint(x uint64) bool
- func (v Value) Pointer() uintptr
- func (v Value) Recv() (x Value, ok bool)
- func (v Value) Send(x Value)
- func (v Value) Set(x Value)
- func (v Value) SetBool(x bool)
- func (v Value) SetComplex(x complex128)
- func (v Value) SetFloat(x float64)
- func (v Value) SetInt(x int64)
- func (v Value) SetLen(n int)
- func (v Value) SetMapIndex(key, val Value)
- func (v Value) SetPointer(x unsafe.Pointer)
- func (v Value) SetString(x string)
- func (v Value) SetUint(x uint64)
- func (v Value) Slice(beg, end int) Value
- func (v Value) String() string
- func (v Value) TryRecv() (x Value, ok bool)
- func (v Value) TrySend(x Value) bool
- func (v Value) Type() Type
- func (v Value) Uint() uint64
- func (v Value) UnsafeAddr() uintptr
- type ValueError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Kind ¶
type Kind uint8
A Kind represents the specific kind of type that a Type represents. The zero Kind is not a valid kind.
type SliceHeader ¶
SliceHeader is the runtime representation of a slice. It cannot be used safely or portably.
type StringHeader ¶
StringHeader is the runtime representation of a string. It cannot be used safely or portably.
type StructField ¶
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. Method(int) Method // NumMethod returns the number of methods in the type's method set. NumMethod() int // Name returns the type's name within its package. // It returns an empty string for unnamed types. Name() string // PkgPath returns the type's package path. // The package path is a full package import path like "container/vector". // PkgPath returns an empty string for unnamed types. 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., vector instead of "container/vector") and is not // guaranteed to be unique among types. To test for equality, // compare the Types directly. String() string // Kind returns the specific kind of this type. Kind() Kind // Implements returns true if the type implements the interface type u. Implements(u Type) bool // AssignableTo returns true if a value of the type is assignable to type u. AssignableTo(u Type) 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 returns true if 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 ... float), then // // t.NumIn() == 2 // t.In(0) is the reflect.Type for "int" // t.In(1) is the reflect.Type for "[]float" // 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, Ptr, 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 first struct field with a name // that satisfies the match function and a boolean indicating if // the field was found. 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 // contains filtered or unexported methods }
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 Value ¶
type Value struct { Internal interface{} InternalMethod int }
Value is the reflection interface to a Go value.
Not all methods apply to all kinds of values. Restrictions, if any, are noted in the documentation for each method. Use the Kind method to find out the kind of value before calling kind-specific methods. Calling a method inappropriate to the kind of type causes a run time panic.
The zero Value represents no value. Its IsValid method returns false, its Kind method returns Invalid, its String method returns "<invalid Value>", and all other methods panic. Most functions and methods never return an invalid value. If one does, its documentation states the conditions explicitly.
The fields of Value are exported so that clients can copy and pass Values around, but they should not be edited or inspected directly. A future language change may make it possible not to export these fields while still keeping Values usable as values.
func Append ¶
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 ¶
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 ¶
Indirect returns the value that v points to. If v is a nil pointer, Indirect returns a nil Value. If v is not a pointer, Indirect returns v.
func MakeSlice ¶
MakeSlice creates a new zero-initialized slice value for the specified slice type, length, and capacity.
func New ¶
New returns a Value representing a pointer to a new zero value for the specified type. That is, the returned Value's Type is PtrTo(t).
func ValueOf ¶
func ValueOf(i interface{}) Value
ValueOf returns a new Value initialized to the concrete value stored in the interface i. ValueOf(nil) returns the zero Value.
func Zero ¶
Zero returns a Value representing a zero value for the specified type. The result is different from the zero value of the Value struct, which represents no value at all. For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0.
func (Value) Addr ¶
Addr returns a pointer value representing the address of v. It panics if CanAddr() returns false. Addr is typically used to obtain a pointer to a struct field or slice element in order to call a method that requires a pointer receiver.
func (Value) Call ¶
Call calls the function v with the input arguments in. For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]). Call panics if v's Kind is not Func. It returns the output results as Values. As in Go, each input argument must be assignable to the type of the function's corresponding input parameter. If v is a variadic function, Call creates the variadic slice parameter itself, copying in the corresponding values.
func (Value) CallSlice ¶
CallSlice calls the variadic function v with the input arguments in, assigning the slice in[len(in)-1] to v's final variadic argument. For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]...). Call panics if v's Kind is not Func or if v is not variadic. It returns the output results as Values. As in Go, each input argument must be assignable to the type of the function's corresponding input parameter.
func (Value) CanAddr ¶
CanAddr returns true if the value's address can be obtained with Addr. Such values are called addressable. A value is addressable if it is an element of a slice, an element of an addressable array, a field of an addressable struct, or the result of dereferencing a pointer. If CanAddr returns false, calling Addr will panic.
func (Value) CanInterface ¶
CanInterface returns true if Interface can be used without panicking.
func (Value) CanSet ¶
CanSet returns true if the value of v can be changed. A Value can be changed only if it is addressable and was not obtained by the use of unexported struct fields. If CanSet returns false, calling Set or any type-specific setter (e.g., SetBool, SetInt64) will panic.
func (Value) Close ¶
func (v Value) Close()
Close closes the channel v. It panics if v's Kind is not Chan.
func (Value) Complex ¶
func (v Value) Complex() complex128
Complex returns v's underlying value, as a complex128. It panics if v's Kind is not Complex64 or Complex128
func (Value) Elem ¶
Elem returns the value that the interface v contains or that the pointer v points to. It panics if v's Kind is not Interface or Ptr. It returns the zero Value if v is nil.
func (Value) Field ¶
Field returns the i'th field of the struct v. It panics if v's Kind is not Struct or i is out of range.
func (Value) FieldByIndex ¶
FieldByIndex returns the nested field corresponding to index. It panics if v's Kind is not struct.
func (Value) FieldByName ¶
FieldByName returns the struct field with the given name. It returns the zero Value if no field was found. It panics if v's Kind is not struct.
func (Value) FieldByNameFunc ¶
FieldByNameFunc returns the struct field with a name that satisfies the match function. It panics if v's Kind is not struct. It returns the zero Value if no field was found.
func (Value) Float ¶
Float returns v's underlying value, as an float64. It panics if v's Kind is not Float32 or Float64
func (Value) Index ¶
Index returns v's i'th element. It panics if v's Kind is not Array or Slice or i is out of range.
func (Value) Int ¶
Int returns v's underlying value, as an int64. It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
func (Value) Interface ¶
func (v Value) Interface() interface{}
Interface returns v's value as an interface{}. If v is a method obtained by invoking Value.Method (as opposed to Type.Method), Interface cannot return an interface value, so it panics.
func (Value) InterfaceData ¶
InterfaceData returns the interface v's value as a uintptr pair. It panics if v's Kind is not Interface.
func (Value) IsNil ¶
IsNil returns true if v is a nil value. It panics if v's Kind is not Chan, Func, Interface, Map, Ptr, or Slice.
func (Value) IsValid ¶
IsValid returns true if v represents a value. It returns false if v is the zero Value. If IsValid returns false, all other methods except String panic. Most functions and methods never return an invalid value. If one does, its documentation states the conditions explicitly.
func (Value) Kind ¶
Kind returns v's Kind. If v is the zero Value (IsValid returns false), Kind returns Invalid.
func (Value) MapIndex ¶
MapIndex returns the value associated with key in the map v. It panics if v's Kind is not Map. It returns the zero Value if key is not found in the map or if v represents a nil map. As in Go, the key's value must be assignable to the map's key type.
func (Value) MapKeys ¶
MapKeys returns a slice containing all the keys present in the map, in unspecified order. It panics if v's Kind is not Map. It returns an empty slice if v represents a nil map.
func (Value) Method ¶
Method returns a function value corresponding to v's i'th method. The arguments to a Call on the returned function should not include a receiver; the returned function will always use v as the receiver. Method panics if i is out of range.
func (Value) NumField ¶
NumField returns the number of fields in the struct v. It panics if v's Kind is not Struct.
func (Value) OverflowComplex ¶
func (v Value) OverflowComplex(x complex128) bool
OverflowComplex returns true if the complex128 x cannot be represented by v's type. It panics if v's Kind is not Complex64 or Complex128.
func (Value) OverflowFloat ¶
OverflowFloat returns true if the float64 x cannot be represented by v's type. It panics if v's Kind is not Float32 or Float64.
func (Value) OverflowInt ¶
OverflowInt returns true if 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 ¶
OverflowUint returns true if 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 ¶
Pointer returns v's value as a uintptr. It returns uintptr instead of unsafe.Pointer so that code using reflect cannot obtain unsafe.Pointers without importing the unsafe package explicitly. It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer.
func (Value) Recv ¶
Recv receives and returns a value from the channel v. It panics if v's Kind is not Chan. The receive blocks until a value is ready. The boolean value ok is true if the value x corresponds to a send on the channel, false if it is a zero value received because the channel is closed.
func (Value) Send ¶
Send sends x on the channel v. It panics if v's kind is not Chan or if x's type is not the same type as v's element type. As in Go, x's value must be assignable to the channel's element type.
func (Value) Set ¶
Set assigns x to the value v. It panics if CanSet returns false. As in Go, x's value must be assignable to v's type.
func (Value) SetBool ¶
SetBool sets v's underlying value. It panics if v's Kind is not Bool or if CanSet() is false.
func (Value) SetComplex ¶
func (v Value) SetComplex(x complex128)
SetComplex sets v's underlying value to x. It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false.
func (Value) SetFloat ¶
SetFloat sets v's underlying value to x. It panics if v's Kind is not Float32 or Float64, or if CanSet() is false.
func (Value) SetInt ¶
SetInt sets v's underlying value to x. It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64, or if CanSet() is false.
func (Value) SetMapIndex ¶
SetMapIndex sets the value associated with key in the map v to val. It panics if v's Kind is not Map. If val is the zero Value, SetMapIndex deletes the key from the map. As in Go, key's value must be assignable to the map's key type, and val's value must be assignable to the map's value type.
func (Value) SetPointer ¶
SetPointer sets the unsafe.Pointer value v to x. It panics if v's Kind is not UnsafePointer.
func (Value) SetString ¶
SetString sets v's underlying value to x. It panics if v's Kind is not String or if CanSet() is false.
func (Value) SetUint ¶
SetUint sets v's underlying value to x. It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64, or if CanSet() is false.
func (Value) String ¶
String returns the string v's underlying value, as a string. String is a special case because of Go's String method convention. Unlike the other getters, it does not panic if v's Kind is not String. Instead, it returns a string of the form "<T value>" where T is v's type.
func (Value) TryRecv ¶
TryRecv attempts to receive a value from the channel v but will not block. It panics if v's Kind is not Chan. If the receive cannot finish without blocking, x is the zero Value. The boolean ok is true if the value x corresponds to a send on the channel, false if it is a zero value received because the channel is closed.
func (Value) TrySend ¶
TrySend attempts to send x on the channel v but will not block. It panics if v's Kind is not Chan. It returns true if the value was sent, false otherwise. As in Go, x's value must be assignable to the channel's element type.
func (Value) Uint ¶
Uint returns v's underlying value, as a uint64. It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
func (Value) UnsafeAddr ¶
UnsafeAddr returns a pointer to v's data. It is for advanced clients that also import the "unsafe" package. It panics if v is not addressable.
type ValueError ¶
A ValueError occurs when a Value method is invoked on a Value that does not support it. Such cases are documented in the description of each method.
func (*ValueError) String ¶
func (e *ValueError) String() string