Documentation ¶
Index ¶
- Constants
- func Copy(dst, src Value) int
- func DeepEqual(x, y interface{}) bool
- func Swapper(slice interface{}) func(i, j int)
- type ChanDir
- type Kind
- type MapIter
- type Method
- type SelectCase
- type SelectDir
- type SliceHeader
- type StringHeader
- type StructField
- type StructTag
- type Type
- type TypeError
- type Value
- func Append(v Value, x ...Value) Value
- func AppendSlice(s, t Value) Value
- func Indirect(v Value) Value
- func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value
- func MakeMap(typ Type) Value
- func MakeMapWithSize(typ Type, n int) Value
- func MakeSlice(typ Type, len, cap int) Value
- func New(typ Type) Value
- func NewAt(typ Type, p unsafe.Pointer) Value
- func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool)
- func ValueOf(i interface{}) Value
- func Zero(typ Type) Value
- func (v Value) Addr() Value
- func (v Value) Bool() bool
- func (v Value) Bytes() []byte
- func (v Value) Call(in []Value) []Value
- func (v Value) CallSlice(in []Value) []Value
- func (v Value) CanAddr() bool
- func (v Value) CanComplex() bool
- func (v Value) CanConvert(t Type) bool
- func (v Value) CanFloat() bool
- func (v Value) CanInt() bool
- func (v Value) CanInterface() bool
- func (v Value) CanSet() bool
- func (v Value) CanUint() bool
- func (v Value) Cap() int
- func (v Value) Clear()
- func (v Value) Close()
- func (v Value) Comparable() bool
- func (v Value) Complex() complex128
- func (v Value) Convert(t Type) Value
- func (v Value) Elem() Value
- func (v Value) Field(i int) Value
- func (v Value) FieldByIndex(index []int) Value
- func (v Value) FieldByIndexErr(index []int) (Value, error)
- func (v Value) FieldByName(name string) Value
- func (v Value) FieldByNameFunc(match func(string) bool) Value
- func (v Value) Float() float64
- func (v Value) Float32() float32
- func (v Value) Grow(n int)
- func (v Value) Index(i int) Value
- func (v Value) Int() int64
- func (v Value) Interface() interface{}
- func (v Value) IsNil() bool
- func (v Value) IsValid() bool
- func (v Value) IsZero() 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) MapRange() *MapIter
- func (v Value) Method(i int) Value
- func (v Value) MethodByName(name string) Value
- func (v Value) NumField() int
- func (v Value) NumMethod() int
- 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) RawType() *rawType
- 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) SetBytes(x []byte)
- func (v Value) SetCap(n int)
- 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, elem Value)
- func (v Value) SetString(x string)
- func (v Value) SetUint(x uint64)
- func (v Value) SetZero()
- func (v Value) Slice(i, j int) Value
- func (v Value) Slice3(i, j, k int) Value
- func (v Value) String() string
- func (v Value) Type() Type
- func (v Value) Uint() uint64
- func (v Value) UnsafeAddr() uintptr
- func (v Value) UnsafePointer() unsafe.Pointer
- type ValueError
Constants ¶
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
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.
Types ¶
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
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 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.
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 StructOf ¶ added in v0.28.0
func StructOf([]StructField) 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.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
func Append ¶ added in v0.14.0
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
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 MakeMapWithSize ¶ added in v0.28.0
MakeMapWithSize creates a new map with the specified type and initial space for approximately n elements.
func New ¶ added in v0.7.0
New is the reflect equivalent of the new(T) keyword, returning a pointer to a new value of the given type.
func Select ¶ added in v0.28.0
func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool)
func (Value) CanComplex ¶ added in v0.28.0
CanComplex reports whether Complex can be used without panicking.
func (Value) CanConvert ¶ added in v0.28.0
func (Value) CanFloat ¶ added in v0.28.0
CanFloat reports whether Float can be used without panicking.
func (Value) CanInterface ¶
func (Value) Cap ¶
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) Comparable ¶ added in v0.28.0
func (Value) Complex ¶
func (v Value) Complex() complex128
func (Value) FieldByIndex ¶ added in v0.14.0
FieldByIndex returns the nested field corresponding to index.
func (Value) FieldByIndexErr ¶ added in v0.23.0
FieldByIndexErr returns the nested field corresponding to index.
func (Value) FieldByName ¶ added in v0.14.0
func (Value) FieldByNameFunc ¶ added in v0.29.0
func (Value) Grow ¶ added in v0.29.0
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) IsNil ¶
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) IsZero ¶ added in v0.27.0
IsZero reports whether v is the zero value for its type. It panics if the argument is invalid.
func (Value) Len ¶
Len returns the length of this value for slices, strings, arrays, channels, and maps. For other types, it panics.
func (Value) MethodByName ¶ added in v0.23.0
func (Value) NumField ¶
NumField returns the number of fields of this struct. It panics for other value types.
func (Value) OverflowFloat ¶ added in v0.18.0
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
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
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 ¶
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) SetComplex ¶
func (v Value) SetComplex(x complex128)
func (Value) SetMapIndex ¶ added in v0.14.0
func (Value) UnsafeAddr ¶ added in v0.28.0
func (Value) UnsafePointer ¶ added in v0.24.0
UnsafePointer returns the underlying pointer of the given value for the following types: chan, map, pointer, unsafe.Pointer, slice, func.
type ValueError ¶
func (*ValueError) Error ¶
func (e *ValueError) Error() string