Go Home Page
The Go Programming Language

Package reflect

import "reflect"

The reflect package 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 an object with interface type Type. That contains a pointer to a struct of type *StructType, *IntType, etc. representing the details of the underlying type. A type switch or type assertion can reveal which.

A call to NewValue creates a Value representing the run-time data; it contains a *StructValue, *IntValue, etc. MakeZero takes a Type and returns a Value representing a zero value for that type.

Package files

deepequal.go type.go value.go

func ArrayCopy

func ArrayCopy(dst, src ArrayOrSliceValue) int

ArrayCopy copies the contents of src into dst until either dst has been filled or src has been exhausted. It returns the number of elements copied. The arrays dst and src must have the same element type.

func DeepEqual

func DeepEqual(a1, a2 interface{}) bool

DeepEqual tests for deep equality. It uses normal == equality where possible but will scan members of arrays, slices, and fields of structs. It correctly handles recursive types.

type ArrayOrSliceType

ArrayOrSliceType is the common interface implemented by both ArrayType and SliceType.

type ArrayOrSliceType interface {
    Type
    Elem() Type
}

type ArrayOrSliceValue

ArrayOrSliceValue is the common interface implemented by both ArrayValue and SliceValue.

type ArrayOrSliceValue interface {
    Value
    Len() int
    Cap() int
    Elem(i int) Value
    // contains unexported methods
}

type ArrayType

ArrayType represents a fixed array type.

type ArrayType struct {
    // contains unexported fields
}

func (*ArrayType) Elem

func (t *ArrayType) Elem() Type

Elem returns the type of the array's elements.

func (*ArrayType) Len

func (t *ArrayType) Len() int

Len returns the number of elements in the array.

type ArrayValue

An ArrayValue represents an array.

type ArrayValue struct {
    // contains unexported fields
}

func (*ArrayValue) Cap

func (v *ArrayValue) Cap() int

Cap returns the capacity of the array (equal to Len()).

func (*ArrayValue) Elem

func (v *ArrayValue) Elem(i int) Value

Elem returns the i'th element of v.

func (*ArrayValue) Len

func (v *ArrayValue) Len() int

Len returns the length of the array.

func (*ArrayValue) Set

func (v *ArrayValue) Set(x *ArrayValue)

Set assigns x to v. The new value x must have the same type as v.

func (*ArrayValue) SetValue

func (v *ArrayValue) SetValue(x Value)

Set sets v to the value x.

type BoolType

BoolType represents a boolean type.

type BoolType struct {
    // contains unexported fields
}

type BoolValue

BoolValue represents a bool value.

type BoolValue struct {
    // contains unexported fields
}

func (*BoolValue) Get

func (v *BoolValue) Get() bool

Get returns the underlying bool value.

func (*BoolValue) Set

func (v *BoolValue) Set(x bool)

Set sets v to the value x.

func (*BoolValue) SetValue

func (v *BoolValue) SetValue(x Value)

Set sets v to the value x.

type ChanDir

ChanDir represents a channel type's direction.

type ChanDir int

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

func (ChanDir) String

func (d ChanDir) String() string

type ChanType

ChanType represents a channel type.

type ChanType struct {
    // contains unexported fields
}

func (*ChanType) Dir

func (t *ChanType) Dir() ChanDir

Dir returns the channel direction.

func (*ChanType) Elem

func (t *ChanType) Elem() Type

Elem returns the channel's element type.

type ChanValue

A ChanValue represents a chan.

type ChanValue struct {
    // contains unexported fields
}

func MakeChan

func MakeChan(typ *ChanType, buffer int) *ChanValue

MakeChan creates a new channel with the specified type and buffer size.

func (*ChanValue) Cap

func (v *ChanValue) Cap() int

func (*ChanValue) Close

func (v *ChanValue) Close()

Close closes the channel.

func (*ChanValue) Closed

func (v *ChanValue) Closed() bool

Closed returns the result of closed(c) on the underlying channel.

func (*ChanValue) Get

func (v *ChanValue) Get() uintptr

Get returns the uintptr value of v. It is mainly useful for printing.

func (*ChanValue) IsNil

func (v *ChanValue) IsNil() bool

IsNil returns whether v is a nil channel.

func (*ChanValue) Len

func (v *ChanValue) Len() int

func (*ChanValue) Recv

func (v *ChanValue) Recv() Value

Recv receives and returns a value from the channel v.

func (*ChanValue) Send

func (v *ChanValue) Send(x Value)

Send sends x on the channel v.

func (*ChanValue) Set

func (v *ChanValue) Set(x *ChanValue)

Set assigns x to v. The new value x must have the same type as v.

func (*ChanValue) SetValue

func (v *ChanValue) SetValue(x Value)

Set sets v to the value x.

func (*ChanValue) TryRecv

func (v *ChanValue) TryRecv() Value

TryRecv attempts to receive a value from the channel v but will not block. It returns the value if one is received, nil otherwise.

func (*ChanValue) TrySend

func (v *ChanValue) TrySend(x Value) bool

TrySend attempts to sends x on the channel v but will not block. It returns true if the value was sent, false otherwise.

type ComplexType

ComplexType represents a complex type.

type ComplexType struct {
    // contains unexported fields
}

type ComplexValue

ComplexValue represents a complex value.

type ComplexValue struct {
    // contains unexported fields
}

func (*ComplexValue) Get

func (v *ComplexValue) Get() complex128

Get returns the underlying complex value.

func (*ComplexValue) Set

func (v *ComplexValue) Set(x complex128)

Set sets v to the value x.

func (*ComplexValue) SetValue

func (v *ComplexValue) SetValue(x Value)

Set sets v to the value x.

type FloatType

FloatType represents a float type.

type FloatType struct {
    // contains unexported fields
}

type FloatValue

FloatValue represents a float value.

type FloatValue struct {
    // contains unexported fields
}

func (*FloatValue) Get

func (v *FloatValue) Get() float64

Get returns the underlying int value.

func (*FloatValue) Overflow

func (v *FloatValue) Overflow(x float64) bool

Overflow returns true if x cannot be represented by the type of v.

func (*FloatValue) Set

func (v *FloatValue) Set(x float64)

Set sets v to the value x.

func (*FloatValue) SetValue

func (v *FloatValue) SetValue(x Value)

Set sets v to the value x.

type FuncType

FuncType represents a function type.

type FuncType struct {
    // contains unexported fields
}

func (*FuncType) DotDotDot

func (t *FuncType) DotDotDot() bool

DotDotDot returns true if the final function input parameter is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's underlying static type []T.

For concreteness, if t is 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.DotDotDot() == true

func (*FuncType) In

func (t *FuncType) In(i int) Type

In returns the type of the i'th function input parameter.

func (*FuncType) NumIn

func (t *FuncType) NumIn() int

NumIn returns the number of input parameters.

func (*FuncType) NumOut

func (t *FuncType) NumOut() int

NumOut returns the number of function output parameters.

func (*FuncType) Out

func (t *FuncType) Out(i int) Type

Out returns the type of the i'th function output parameter.

type FuncValue

A FuncValue represents a function value.

type FuncValue struct {
    // contains unexported fields
}

func (*FuncValue) Call

func (fv *FuncValue) Call(in []Value) []Value

Call calls the function fv with input parameters in. It returns the function's output parameters as Values.

func (*FuncValue) Get

func (v *FuncValue) Get() uintptr

Get returns the uintptr value of v. It is mainly useful for printing.

func (*FuncValue) IsNil

func (v *FuncValue) IsNil() bool

IsNil returns whether v is a nil function.

func (*FuncValue) Set

func (v *FuncValue) Set(x *FuncValue)

Set assigns x to v. The new value x must have the same type as v.

func (*FuncValue) SetValue

func (v *FuncValue) SetValue(x Value)

Set sets v to the value x.

type IntType

IntType represents a signed integer type.

type IntType struct {
    // contains unexported fields
}

type IntValue

IntValue represents an int value.

type IntValue struct {
    // contains unexported fields
}

func (*IntValue) Get

func (v *IntValue) Get() int64

Get returns the underlying int value.

func (*IntValue) Overflow

func (v *IntValue) Overflow(x int64) bool

Overflow returns true if x cannot be represented by the type of v.

func (*IntValue) Set

func (v *IntValue) Set(x int64)

Set sets v to the value x.

func (*IntValue) SetValue

func (v *IntValue) SetValue(x Value)

Set sets v to the value x.

type InterfaceType

InterfaceType represents an interface type.

type InterfaceType struct {
    // contains unexported fields
}

func (*InterfaceType) Method

func (t *InterfaceType) Method(i int) (m Method)

Method returns the i'th interface method.

func (*InterfaceType) NumMethod

func (t *InterfaceType) NumMethod() int

NumMethod returns the number of interface methods.

type InterfaceValue

An InterfaceValue represents an interface value.

type InterfaceValue struct {
    // contains unexported fields
}

func (*InterfaceValue) Elem

func (v *InterfaceValue) Elem() Value

Elem returns the concrete value stored in the interface value v.

func (*InterfaceValue) IsNil

func (v *InterfaceValue) IsNil() bool

IsNil returns whether v is a nil interface value.

func (*InterfaceValue) Method

func (v *InterfaceValue) Method(i int) *FuncValue

Method returns a FuncValue corresponding to v's i'th method. The arguments to a Call on the returned FuncValue should not include a receiver; the FuncValue will use v as the receiver.

func (*InterfaceValue) Set

func (v *InterfaceValue) Set(x Value)

Set assigns x to v.

func (*InterfaceValue) SetValue

func (v *InterfaceValue) SetValue(x Value)

Set sets v to the value x.

type Kind

A Kind represents the specific kind of type that a Type represents. For numeric types, the Kind gives more information than the Type's dynamic type. For example, the Type of a float32 is FloatType, but the Kind is Float32.

The zero Kind is not a valid kind.

type Kind uint8

const (
    Bool Kind = 1 + iota
    Int
    Int8
    Int16
    Int32
    Int64
    Uint
    Uint8
    Uint16
    Uint32
    Uint64
    Uintptr
    Float
    Float32
    Float64
    Complex
    Complex64
    Complex128
    Array
    Chan
    Func
    Interface
    Map
    Ptr
    Slice
    String
    Struct
    UnsafePointer
)

func (Kind) String

func (k Kind) String() string

type MapType

MapType represents a map type.

type MapType struct {
    // contains unexported fields
}

func (*MapType) Elem

func (t *MapType) Elem() Type

Elem returns the map element type.

func (*MapType) Key

func (t *MapType) Key() Type

Key returns the map key type.

type MapValue

A MapValue represents a map value.

type MapValue struct {
    // contains unexported fields
}

func MakeMap

func MakeMap(typ *MapType) *MapValue

MakeMap creates a new map of the specified type.

func (*MapValue) Elem

func (v *MapValue) Elem(key Value) Value

Elem returns the value associated with key in the map v. It returns nil if key is not found in the map.

func (*MapValue) Get

func (v *MapValue) Get() uintptr

Get returns the uintptr value of v. It is mainly useful for printing.

func (*MapValue) IsNil

func (v *MapValue) IsNil() bool

IsNil returns whether v is a nil map value.

func (*MapValue) Keys

func (v *MapValue) Keys() []Value

Keys returns a slice containing all the keys present in the map, in unspecified order.

func (*MapValue) Len

func (v *MapValue) Len() int

Len returns the number of keys in the map v.

func (*MapValue) Set

func (v *MapValue) Set(x *MapValue)

Set assigns x to v. The new value x must have the same type as v.

func (*MapValue) SetElem

func (v *MapValue) SetElem(key, val Value)

SetElem sets the value associated with key in the map v to val. If val is nil, Put deletes the key from map.

func (*MapValue) SetValue

func (v *MapValue) SetValue(x Value)

Set sets v to the value x.

type Method

Method represents a single method.

type Method struct {
    PkgPath string // empty for uppercase Name
    Name    string
    Type    *FuncType
    Func    *FuncValue
}

type PtrType

PtrType represents a pointer type.

type PtrType struct {
    // contains unexported fields
}

func (*PtrType) Elem

func (t *PtrType) Elem() Type

Elem returns the pointer element type.

type PtrValue

A PtrValue represents a pointer.

type PtrValue struct {
    // contains unexported fields
}

func (*PtrValue) Elem

func (v *PtrValue) Elem() Value

Elem returns the value that v points to. If v is a nil pointer, Elem returns a nil Value.

func (*PtrValue) Get

func (v *PtrValue) Get() uintptr

Get returns the uintptr value of v. It is mainly useful for printing.

func (*PtrValue) IsNil

func (v *PtrValue) IsNil() bool

IsNil returns whether v is a nil pointer.

func (*PtrValue) PointTo

func (v *PtrValue) PointTo(x Value)

PointTo changes v to point to x. If x is a nil Value, PointTo sets v to nil.

func (*PtrValue) Set

func (v *PtrValue) Set(x *PtrValue)

Set assigns x to v. The new value x must have the same type as v.

func (*PtrValue) SetValue

func (v *PtrValue) SetValue(x Value)

Set sets v to the value x.

type SliceHeader

runtime representation of slice

type SliceHeader struct {
    Data uintptr
    Len  int
    Cap  int
}

type SliceType

SliceType represents a slice type.

type SliceType struct {
    // contains unexported fields
}

func (*SliceType) Elem

func (t *SliceType) Elem() Type

Elem returns the type of the slice's elements.

type SliceValue

A SliceValue represents a slice.

type SliceValue struct {
    // contains unexported fields
}

func MakeSlice

func MakeSlice(typ *SliceType, len, cap int) *SliceValue

MakeSlice creates a new zero-initialized slice value for the specified slice type, length, and capacity.

func (*SliceValue) Cap

func (v *SliceValue) Cap() int

Cap returns the capacity of the slice.

func (*SliceValue) Elem

func (v *SliceValue) Elem(i int) Value

Elem returns the i'th element of v.

func (*SliceValue) Get

func (v *SliceValue) Get() uintptr

Get returns the uintptr address of the v.Cap()'th element. This gives the same result for all slices of the same array. It is mainly useful for printing.

func (*SliceValue) IsNil

func (v *SliceValue) IsNil() bool

IsNil returns whether v is a nil slice.

func (*SliceValue) Len

func (v *SliceValue) Len() int

Len returns the length of the slice.

func (*SliceValue) Set

func (v *SliceValue) Set(x *SliceValue)

Set assigns x to v. The new value x must have the same type as v.

func (*SliceValue) SetLen

func (v *SliceValue) SetLen(n int)

SetLen changes the length of v. The new length n must be between 0 and the capacity, inclusive.

func (*SliceValue) SetValue

func (v *SliceValue) SetValue(x Value)

Set sets v to the value x.

func (*SliceValue) Slice

func (v *SliceValue) Slice(beg, end int) *SliceValue

Slice returns a sub-slice of the slice v.

type StringHeader

StringHeader is the runtime representation of a string.

type StringHeader struct {
    Data uintptr
    Len  int
}

type StringType

StringType represents a string type.

type StringType struct {
    // contains unexported fields
}

type StringValue

StringValue represents a string value.

type StringValue struct {
    // contains unexported fields
}

func (*StringValue) Get

func (v *StringValue) Get() string

Get returns the underlying string value.

func (*StringValue) Set

func (v *StringValue) Set(x string)

Set sets v to the value x.

func (*StringValue) SetValue

func (v *StringValue) SetValue(x Value)

Set sets v to the value x.

type StructField

type StructField struct {
    PkgPath   string // empty for uppercase Name
    Name      string
    Type      Type
    Tag       string
    Offset    uintptr
    Index     []int
    Anonymous bool
}

type StructType

StructType represents a struct type.

type StructType struct {
    // contains unexported fields
}

func (*StructType) Field

func (t *StructType) Field(i int) (f StructField)

Field returns the i'th struct field.

func (*StructType) FieldByIndex

func (t *StructType) FieldByIndex(index []int) (f StructField)

FieldByIndex returns the nested field corresponding to index.

func (*StructType) FieldByName

func (t *StructType) FieldByName(name string) (f StructField, present bool)

FieldByName returns the struct field with the given name and a boolean to indicate if the field was found.

func (*StructType) FieldByNameFunc

func (t *StructType) FieldByNameFunc(match func(string) bool) (f StructField, present bool)

FieldByNameFunc returns the struct field with a name that satisfies the match function and a boolean to indicate if the field was found.

func (*StructType) NumField

func (t *StructType) NumField() int

NumField returns the number of struct fields.

type StructValue

A StructValue represents a struct value.

type StructValue struct {
    // contains unexported fields
}

func (*StructValue) Field

func (v *StructValue) Field(i int) Value

Field returns the i'th field of the struct.

func (*StructValue) FieldByIndex

func (t *StructValue) FieldByIndex(index []int) (v Value)

FieldByIndex returns the nested field corresponding to index.

func (*StructValue) FieldByName

func (t *StructValue) FieldByName(name string) Value

FieldByName returns the struct field with the given name. The result is nil if no field was found.

func (*StructValue) FieldByNameFunc

func (t *StructValue) FieldByNameFunc(match func(string) bool) Value

FieldByNameFunc returns the struct field with a name that satisfies the match function. The result is nil if no field was found.

func (*StructValue) NumField

func (v *StructValue) NumField() int

NumField returns the number of fields in the struct.

func (*StructValue) Set

func (v *StructValue) Set(x *StructValue)

Set assigns x to v. The new value x must have the same type as v.

func (*StructValue) SetValue

func (v *StructValue) SetValue(x Value)

Set sets v to the value x.

type Type

Type is the runtime representation of a Go type. Every type implements the methods listed here. Some types implement additional interfaces; use a type switch to find out what kind of type a Type is. Each type in a program has a unique Type, so == on Types corresponds to Go's type equality.

type Type interface {
    // 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

    // Name returns the type's name within its package.
    // Name returns an empty string for unnamed types.
    Name() string

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

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

    // Bits returns the size of the type in bits.
    // It is intended for use with numeric types and may overflow
    // when used for composite types.
    Bits() int

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

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

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

    // For non-interface types, Method returns the i'th method with receiver T.
    // For interface types, Method returns the i'th method in the interface.
    // NumMethod returns the number of such methods.
    Method(int) Method
    NumMethod() int
    // contains unexported methods
}

func Typeof

func Typeof(i interface{}) Type

Typeof returns the reflection Type of the value in the interface{}.

type UintType

UintType represents a uint type.

type UintType struct {
    // contains unexported fields
}

type UintValue

UintValue represents a uint value.

type UintValue struct {
    // contains unexported fields
}

func (*UintValue) Get

func (v *UintValue) Get() uint64

Get returns the underlying uuint value.

func (*UintValue) Overflow

func (v *UintValue) Overflow(x uint64) bool

Overflow returns true if x cannot be represented by the type of v.

func (*UintValue) Set

func (v *UintValue) Set(x uint64)

Set sets v to the value x.

func (*UintValue) SetValue

func (v *UintValue) SetValue(x Value)

Set sets v to the value x.

type UnsafePointerType

UnsafePointerType represents an unsafe.Pointer type.

type UnsafePointerType struct {
    // contains unexported fields
}

type UnsafePointerValue

UnsafePointerValue represents an unsafe.Pointer value.

type UnsafePointerValue struct {
    // contains unexported fields
}

func (*UnsafePointerValue) Get

func (v *UnsafePointerValue) Get() uintptr

Get returns the underlying uintptr value. Get returns uintptr, not unsafe.Pointer, so that programs that do not import "unsafe" cannot obtain a value of unsafe.Pointer type from "reflect".

func (*UnsafePointerValue) Set

func (v *UnsafePointerValue) Set(x unsafe.Pointer)

Set sets v to the value x.

func (*UnsafePointerValue) SetValue

func (v *UnsafePointerValue) SetValue(x Value)

Set sets v to the value x.

type Value

Value is the common interface to reflection values. The implementations of Value (e.g., ArrayValue, StructValue) have additional type-specific methods.

type Value interface {
    // Type returns the value's type.
    Type() Type

    // Interface returns the value as an interface{}.
    Interface() interface{}

    // CanSet returns whether the value can be changed.
    // Values obtained by the use of non-exported struct fields
    // can be used in Get but not Set.
    // If CanSet() returns false, calling the type-specific Set
    // will cause a crash.
    CanSet() bool

    // SetValue assigns v to the value; v must have the same type as the value.
    SetValue(v Value)

    // Addr returns a pointer to the underlying data.
    // It is for advanced clients that also
    // import the "unsafe" package.
    Addr() uintptr

    // Method returns a FuncValue corresponding to the value's i'th method.
    // The arguments to a Call on the returned FuncValue
    // should not include a receiver; the FuncValue will use
    // the value as the receiver.
    Method(i int) *FuncValue
    // contains unexported methods
}

func Indirect

func Indirect(v Value) Value

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 MakeZero

func MakeZero(typ Type) Value

MakeZero returns a zero Value for the specified Type.

func NewValue

func NewValue(i interface{}) Value

NewValue returns a new Value initialized to the concrete value stored in the interface i. NewValue(nil) returns nil.