?
This document uses PHP Chinese website manual Release
import "reflect"
概述
索引
reflect 包了實(shí)現(xiàn)運(yùn)行時(shí)反射,允許程序使用任意類型操作對(duì)象。典型的用法是用靜態(tài)類型接口{}取值并通過調(diào)用返回類型的 TypeOf 來提取其動(dòng)態(tài)類型信息。
對(duì) ValueOf 的調(diào)用返回表示運(yùn)行時(shí)數(shù)據(jù)的值。Zero 采用 Type 并返回表示該類型的零值的 Value 。
func Copy(dst, src Value) int
func DeepEqual(x, y interface{}) bool
func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool)
func Swapper(slice interface{}) func(i, j int)
type ChanDir
func (d ChanDir) String() string
type Kind
func (k Kind) String() string
type Method
type SelectCase
type SelectDir
type SliceHeader
type StringHeader
type StructField
type StructTag
func (tag StructTag) Get(key string) string
func (tag StructTag) Lookup(key string) (value string, ok bool)
type Type
func ArrayOf(count int, elem Type) Type
func ChanOf(dir ChanDir, t Type) Type
func FuncOf(in, out []Type, variadic bool) Type
func MapOf(key, elem Type) Type
func PtrTo(t Type) Type
func SliceOf(t Type) Type
func StructOf(fields []StructField) Type
func TypeOf(i interface{}) 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 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 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) CanInterface() bool
func (v Value) CanSet() bool
func (v Value) Cap() int
func (v Value) Close()
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) 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() (i 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) MethodByName(name string) Value
func (v Value) NumField() int
func (v Value) NumMethod() 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) 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, 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(i, j int) Value
func (v Value) Slice3(i, j, k 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
func (e *ValueError) Error() string
Bugs
deepequal.go makefunc.go swapper.go type.go value.go
func Copy(dst, src Value) int
復(fù)制將 src 的內(nèi)容復(fù)制到 dst ,直到 dst 被填充或 src 已用盡。它返回復(fù)制元素的數(shù)量。Dst 和 src 都必須有 Slice 或 Array 類型,而 dst 和 src 必須具有相同的元素類型。
func DeepEqual(x, y interface{}) bool
DeepEqual 報(bào)告 x 和 y 是否 “deeply equal” ,定義如下。如果以下情況之一適用,相同類型的兩個(gè)值是非常相等的。不同類型的值永遠(yuǎn)不會(huì)相等。
當(dāng)它們的對(duì)應(yīng)元素非常相等時(shí),數(shù)組值是非常相等的。
如果它們的導(dǎo)出和未導(dǎo)出的相應(yīng)字段深度相等,那么結(jié)構(gòu)值是相等的。
如果兩者均為零,則 Func 值非常相等;否則他們并不是很平等。
如果接口值具有深刻的等于具體值,則接口值是相等的。
當(dāng)滿足以下所有條件時(shí),地圖值是完全相同的:它們都是零或兩者都是非零,它們具有相同的長度,并且它們是相同的地圖對(duì)象或它們對(duì)應(yīng)的鍵(使用 Go 相等匹配)映射到深度相等的價(jià)值。
如果使用 Go 的==運(yùn)算符或者指向深度相等的值,則指針值相等,如果它們相等則指針值相等。
當(dāng)滿足以下所有條件時(shí),slice 值是相等的:它們都是零或者都是非零,它們具有相同的長度,并且它們指向相同基礎(chǔ)數(shù)組的相同初始條目(即&x0 == &y0)或其相應(yīng)的元素(長度)是非常相等的。請(qǐng)注意,非零空片和零片(例如,[] byte {}和[] []byte(nil))并不完全相等。
其他值 - 數(shù)字,布爾,字符串和通道 - 如果使用Go的==運(yùn)算符相等,則它們是相等的。
一般而言,DeepEqual 是 Go 的==運(yùn)算符的遞歸放松。然而,這個(gè)想法是不可能實(shí)現(xiàn)的,沒有一些不一致。具體而言,一個(gè)值可能與自身不相等,或者是因?yàn)樗?func 類型(通常無法比較),或者因?yàn)樗歉↑c(diǎn)型 NaN 值(在浮點(diǎn)比較中不等于自身),或者因?yàn)樗歉↑c(diǎn)型它是包含這種值的數(shù)組,結(jié)構(gòu)或接口。另一方面,指針值總是等于它們自己,即使它們指向或包含這樣的有問題的值,因?yàn)樗鼈兪褂?Go 的==運(yùn)算符比較相等,并且這是一個(gè)足夠的條件,無論內(nèi)容如何,它都是完全相等的。DeepEqual 已被定義,以便相同的捷徑適用于切片和貼圖:如果 x 和 y 是相同的切片或相同的貼圖,則無論內(nèi)容如何,它們都是完全相同的。
當(dāng) DeepEqual 遍歷數(shù)據(jù)值時(shí),它可能會(huì)找到一個(gè)循環(huán)。第二次和后來的時(shí)間,DeepEqual 比較之前比較過的兩個(gè)指針值,它將這些值視為相等,而不是檢查它們指向的值。這確保了 DeepEqual 終止。
func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool)
選擇執(zhí)行由案例列表描述的選擇操作。與 Go select 語句一樣,它會(huì)阻塞,直到至少有一種情況可以繼續(xù),進(jìn)行統(tǒng)一的偽隨機(jī)選擇,然后執(zhí)行該情況。它返回所選情況的索引,如果是接收操作,則返回接收到的值,并返回一個(gè)布爾值,指示該值是否與通道上的發(fā)送相對(duì)應(yīng)(與通道關(guān)閉時(shí)接收的零值相對(duì))。
func Swapper(slice interface{}) func(i, j int)
Swapper 返回一個(gè)函數(shù),它交換提供的 slice 中的元素。
如果提供的接口不是切片,Swapper 會(huì)發(fā)生混亂。
ChanDir 代表了一種渠道類型的方向。
type ChanDir int
const ( RecvDir ChanDir = 1 << iota // <-chan SendDir // chan<- BothDir = RecvDir | SendDir // chan)
func (d ChanDir) String() string
類型表示類型代表的特定種類。零種類不是一種有效的種類。
type Kind uint
const ( Invalid Kind = iota Bool Int Int8 Int16 Int32 Int64 Uint Uint8 Uint16 Uint32 Uint64 Uintptr Float32 Float64 Complex64 Complex128 Array Chan Func Interface Map Ptr Slice String Struct UnsafePointer)
func (k Kind) String() string
Method 代表一種方法。
type Method struct { // Name is the method name. // 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 Name string PkgPath string Type Type // method type Func Value // func with receiver as first argument Index int // index for Type.Method}
SelectCase 在選擇操作中描述了一個(gè)案例。這種情況取決于 Dir ,溝通方向。
如果 Dir 是 SelectDefault ,則表示默認(rèn)情況。Chan 和 Send 必須為零值。
如果 Dir 是 SelectSend ,則表示發(fā)送操作。通常 Chan 的基礎(chǔ)值必須是一個(gè)通道,并且 Send 的基礎(chǔ)值必須可分配給通道的元素類型。作為一種特殊情況,如果 Chan 是零值,則忽略該情況,并且發(fā)送字段也將被忽略,并且可以是零或非零。
如果 Dir 是 SelectRecv ,則表示接收操作。通常陳的基礎(chǔ)價(jià)值必須是一個(gè)渠道和發(fā)送必須是一個(gè)零值。如果 Chan 是零值,則該情況將被忽略,但發(fā)送必須仍為零值。當(dāng)選擇接收操作時(shí),接收的值由選擇返回。
type SelectCase struct { Dir SelectDir // direction of case Chan Value // channel to use (for send or receive) Send Value // value to send (for send)}
SelectDir 描述選擇案例的通信方向。
type SelectDir int
const ( SelectSend SelectDir // case Chan <- Send SelectRecv // case <-Chan: SelectDefault // default)
SliceHeader 是切片的運(yùn)行時(shí)表示。它不能被安全地使用或移植,其代表可能會(huì)在以后的版本中發(fā)生變化。此外,數(shù)據(jù)字段不足以保證它所引用的數(shù)據(jù)不會(huì)被垃圾收集,因此程序必須保持一個(gè)單獨(dú)的,正確類型的指向底層數(shù)據(jù)的指針。
type SliceHeader struct { Data uintptr Len int Cap int}
StringHeader 是字符串的運(yùn)行時(shí)表示。它不能被安全地使用或移植,其代表可能會(huì)在以后的版本中發(fā)生變化。此外,數(shù)據(jù)字段不足以保證它所引用的數(shù)據(jù)不會(huì)被垃圾收集,因此程序必須保持一個(gè)單獨(dú)的,正確類型的指向底層數(shù)據(jù)的指針。
type StringHeader struct { Data uintptr Len int}
StructField 描述結(jié)構(gòu)中的單個(gè)字段。
type StructField struct { // Name is the field name. Name string // PkgPath is the package path that qualifies a lower case (unexported) // field name. It is empty for upper case (exported) field names. // See https://golang.org/ref/spec#Uniqueness_of_identifiers PkgPath string Type Type // field type Tag StructTag // field tag string Offset uintptr // offset within struct, in bytes Index []int // index sequence for Type.FieldByIndex Anonymous bool // is an embedded field}
StructTag 是 struct 字段中的標(biāo)記字符串。
按照慣例,標(biāo)記字符串是可選空間分隔鍵:“值”對(duì)的串聯(lián)。每個(gè)鍵都是由空格 (U+0020 ' '),引號(hào)(U+0022 '"')和冒號(hào)(U+003A ':')以外的非控制字符組成的非空字符串。使用U + 0022'“'字符和 Go 字符串文字語法。
type StructTag string
func (tag StructTag) Get(key string) string
獲取返回與標(biāo)記字符串中的鍵關(guān)聯(lián)的值。如果標(biāo)簽中沒有這樣的密鑰,則 Get 返回空字符串。如果標(biāo)簽不具有傳統(tǒng)格式,則 Get 返回的值是未指定的。要確定標(biāo)記是否顯式設(shè)置為空字符串,請(qǐng)使用查找。
func (tag StructTag) Lookup(key string) (value string, ok bool)
查找將返回與標(biāo)記字符串中的鍵關(guān)聯(lián)的值。如果密鑰存在于標(biāo)簽中,則返回值(可能為空)。否則,返回的值將是空字符串。ok 返回值報(bào)告該值是否在標(biāo)記字符串中明確設(shè)置。如果標(biāo)記不具有傳統(tǒng)格式,則查找返回的值未指定。
Type 是 Go 類型的表示。
并非所有方法都適用于各種類型。每種方法的文檔中都會(huì)注明限制(如果有的話)。在調(diào)用種類特定的方法之前,使用 Kind 方法找出類型的類型。調(diào)用不適合這種類型的方法會(huì)導(dǎo)致運(yùn)行時(shí)恐慌。
類型值具有可比性,如使用==運(yùn)算符。如果兩個(gè)類型的值表示相同類型,則這兩個(gè)值相等
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 // 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. // It returns an empty string for unnamed types. Name() string // PkgPath returns a named 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 unnamed (*T, struct{}, []int), // 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, 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 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 anonymous 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 anonymous 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 // contains filtered or unexported methods}
func ArrayOf(count int, elem Type) Type
ArrayOf 使用給定的計(jì)數(shù)和元素類型返回?cái)?shù)組類型。例如,如果 t 表示 int ,則 ArrayOf(5, t) 表示 5int 。
如果生成的類型會(huì)比可用的地址空間大, ArrayOf 恐慌。
func ChanOf(dir ChanDir, t Type) Type
ChanOf 返回具有給定方向和元素類型的通道類型。例如,如果 t 表示 int ,則 ChanOf(RecvDir,t) 表示 <-chan int 。
gc 運(yùn)行時(shí)在通道元素類型上施加了 64 kB 的限制。如果t的大小等于或超過此限制, ChanOf 恐慌。
func FuncOf(in, out []Type, variadic bool) Type
FuncOf 返回給定參數(shù)和結(jié)果類型的函數(shù)類型。例如,如果 k 表示 int 并且 e 表示字符串,則 FuncOf([] Type {k},[] Type {e},false) 表示 func(int) 字符串。
可變參數(shù)控制函數(shù)是否可變。如果 inlen(in)-1 不代表切片并且可變參數(shù)為真,則 FuncOf 發(fā)生混亂。
func MapOf(key, elem Type) Type
MapOf 使用給定的鍵和元素類型返回地圖類型。例如,如果 k 表示 int 并且 e 表示字符串,則 MapOf(k, e) 表示 mapintstring 。
如果鍵類型不是有效的映射鍵類型(也就是說,如果它不執(zhí)行 Go 的==運(yùn)算符),則 MapOf 發(fā)生混亂。
func PtrTo(t Type) Type
PtrTo 返回指向元素t的指針類型。例如,如果 t 表示類型 Foo ,則 PtrT(t) 表示 * Foo 。
func SliceOf(t Type) Type
SliceOf 返回元素類型為 t 的切片類型。例如,如果 t 表示 int , SliceOf(t) 表示 [] int 。
func StructOf(fields []StructField) Type
StructOf 返回包含字段的結(jié)構(gòu)類型。偏移量和索引字段被忽略和計(jì)算,就像編譯器一樣。
StructOf 目前不會(huì)為嵌入字段生成封裝器方法。未來版本中可能會(huì)取消此限制。
func TypeOf(i interface{}) Type
TypeOf 返回表示i的動(dòng)態(tài)類型的反射 Type 。如果我是一個(gè)無接口值, TypeOf 返回 nil 。
Value 是 Go 值的反射界面。
并非所有方法都適用于各種價(jià)值。每種方法的文檔中都會(huì)注明限制(如果有的話)。在調(diào)用種類特定的方法之前,使用 Kind 方法找出這種值。調(diào)用不適合這種類型的方法會(huì)導(dǎo)致運(yùn)行時(shí)間恐慌。
零值表示沒有值。它的 IsValid 方法返回 false ,其 Kind 方法返回Invalid,其 String 方法返回 “<invalid Value>” ,所有其他方法都會(huì)出現(xiàn)混亂。大多數(shù)函數(shù)和方法永遠(yuǎn)不會(huì)返回?zé)o效值。如果有的話,其文件明確說明條件。
一個(gè)值可以被多個(gè) goroutine 同時(shí)使用,前提是 Go 值可以同時(shí)用于等價(jià)的直接操作。
要比較兩個(gè)值,請(qǐng)比較 Interface 方法的結(jié)果。在兩個(gè)值上使用==不會(huì)比較它們表示的基礎(chǔ)值。
type Value struct { // contains filtered or unexported fields}
func Append(s Value, x ...Value) Value
追加將值 x 附加到切片 s 并返回結(jié)果切片。和 Go 一樣,每個(gè) x 的值必須可分配給切片的元素類型。
func AppendSlice(s, t Value) Value
AppendSlice 將切片 t 附加到切片 s 并返回結(jié)果切片。切片 s 和 t 必須具有相同的元素類型。
func Indirect(v Value) Value
間接返回 v 指向的值。如果 v 是一個(gè)零指針,則間接返回一個(gè)零值。如果 v 不是指針,則間接返回 v 。
func MakeChan(typ Type, buffer int) Value
MakeChan 使用指定的類型和緩沖區(qū)大小創(chuàng)建一個(gè)新的通道。
func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value
MakeFunc 返回一個(gè)包裝函數(shù) fn 的給定類型的新函數(shù)。被調(diào)用時(shí),該新功能執(zhí)行以下操作:
- converts its arguments to a slice of Values.- runs results := fn(args).- returns the results as a slice of Values, one per formal result.
實(shí)現(xiàn) fn 可以假定參數(shù) Value slice 具有由 typ 給出的參數(shù)的數(shù)量和類型。如果 typ 描述了一個(gè)可變參數(shù)函數(shù),那么最終的值本身就是一個(gè)表示可變參數(shù)的分片,就像可變參數(shù)函數(shù)的主體一樣。結(jié)果由 fn 返回的值切片必須具有由 typ 給出的結(jié)果的數(shù)量和類型。
Value.Call 方法允許調(diào)用者根據(jù)值調(diào)用類型化函數(shù); 相比之下, MakeFunc 允許調(diào)用者根據(jù)值實(shí)現(xiàn)類型化函數(shù)。
文檔的 Examples 部分包含了如何使用 MakeFunc 為不同類型構(gòu)建交換函數(shù)的說明。
func MakeMap(typ Type) Value
MakeMap 使用指定的類型創(chuàng)建一個(gè)新的地圖。
func MakeMapWithSize(typ Type, n int) Value
MakeMapWithSize 為約 n 個(gè)元素創(chuàng)建一個(gè)具有指定類型和初始空間的新映射。
func MakeSlice(typ Type, len, cap int) Value
MakeSlice 為指定的切片類型,長度和容量創(chuàng)建新的零初始化切片值。
func New(typ Type) Value
New 返回一個(gè) Value ,表示指向指定類型的新零值的指針。也就是說,返回值的類型是 PtrTo(typ) 。
func NewAt(typ Type, p unsafe.Pointer) Value
NewAt 返回一個(gè)表示指向指定類型值的指針的值,使用 p 作為指針。
func ValueOf(i interface{}) Value
ValueOf 返回一個(gè)新值,初始化為存儲(chǔ)在接口 i 中的具體值。ValueOf(nil) 返回零值。
func Zero(typ Type) Value
Zero 返回表示指定類型的零值的值。結(jié)果與 Value 結(jié)構(gòu)的零值不同,后者完全不表示任何值。例如, Zero(TypeOf(42)) 返回一個(gè) Value with Kind Int 和值0。返回的值既不可尋址也不可設(shè)置。
func (v Value) Addr() Value
Addr 返回一個(gè)代表 v 的地址的指針值,如果 CanAddr() 返回 false ,則會(huì)發(fā)生混亂。 Addr 通常用于獲取指向結(jié)構(gòu)體字段或切片元素的指針,以便調(diào)用需要指針接收器的方法。
func (v Value) Bool() bool
布爾返回 v 的基礎(chǔ)價(jià)值。如果 v 的種類不是 Bool ,它會(huì)感到恐慌。
func (v Value) Bytes() []byte
字節(jié)返回 v 的基礎(chǔ)值。如果 v 的基礎(chǔ)值不是一個(gè)字節(jié)片段,它會(huì)發(fā)生混亂。
func (v Value) Call(in []Value) []Value
例如,如果 len(in)== 3,v.Call(in) 表示 Go 調(diào)用 v(in0,in1,in2) 。如果 v 的 Kind 不是 Func ,則調(diào)用 panics 。它將輸出結(jié)果作為值返回。和 Go 一樣,每個(gè)輸入?yún)?shù)必須可以分配給函數(shù)相應(yīng)輸入?yún)?shù)的類型。如果 v 是一個(gè)可變參數(shù)函數(shù), Call 將自己創(chuàng)建可變參數(shù)切片參數(shù),并復(fù)制相應(yīng)的值。
func (v Value) CallSlice(in []Value) []Value
CallSlice 調(diào)用帶有輸入?yún)?shù)的可變參數(shù)函數(shù) v ,將片段 inlen(in)-1 分配給 v 的最終可變參數(shù)。例如,如果 len(in)== 3 , v.CallSlice(in) 表示 Go 呼叫 v(in0,in1,in2 ...) 。如果 v 的 Kind 不是 Func 或者 v 不是可變參數(shù), CallSlice 會(huì)發(fā)生混亂。它將輸出結(jié)果作為值返回。和 Go 一樣,每個(gè)輸入?yún)?shù)必須可以分配給函數(shù)相應(yīng)輸入?yún)?shù)的類型。
func (v Value) CanAddr() bool
CanAddr 報(bào)告是否可以使用 Addr 獲取該值的地址。這些值稱為可尋址的。如果值是切片的元素,可尋址數(shù)組的元素,可尋址結(jié)構(gòu)的字段或取消引用指針的結(jié)果,則該值是可尋址的。如果 CanAddr 返回 false ,調(diào)用 Addr 將會(huì)發(fā)生混亂。
func (v Value) CanInterface() bool
CanInterface 報(bào)告 Interface 是否可以在沒有恐慌的情況下使用。
func (v Value) CanSet() bool
CanSet 報(bào)告 v 的值是否可以改變。值只能在可尋址的情況下更改,并且不能通過使用未導(dǎo)出的結(jié)構(gòu)字段獲取。如果 CanSet 返回 false ,則調(diào)用 Set 或任何類型特定的 setter (例如 SetBool ,SetInt )將會(huì)發(fā)生混亂。
func (v Value) Cap() int
Cap 返回 v 的容量。如果 v 的 Kind 不是 Array,Chan 或 Slice ,它會(huì)發(fā)生混亂。
func (v Value) Close()
關(guān)閉關(guān)閉頻道 v 。如果 v 的 Kind 不是 Chan ,它會(huì)發(fā)生混亂。
func (v Value) Complex() complex128
綜合體返回 v 的底層價(jià)值,作為一個(gè)綜合體128。如果 v 的 Kind 不是 Complex64 或 Complex128 ,它會(huì)發(fā)生混亂
func (v Value) Convert(t Type) Value
轉(zhuǎn)換返回轉(zhuǎn)換為類型 t 的值 v 。如果通常的 Go 轉(zhuǎn)換規(guī)則不允許將值 v 轉(zhuǎn)換為類型 t ,則轉(zhuǎn)換恐慌。
func (v Value) Elem() Value
Elem 返回接口 v 包含的值或指針 v 指向的值。如果 v 的 Kind 不是 Interface 或 Ptr ,它會(huì)發(fā)生混亂。如果 v 為零,它返回零值。
func (v Value) Field(i int) Value
Field 返回 struct v 的第 i 個(gè)字段,如果 v 的 Kind 不是 Struct 或者我超出范圍,它就會(huì)發(fā)生混亂。
func (v Value) FieldByIndex(index []int) Value
FieldByIndex 返回索引對(duì)應(yīng)的嵌套字段。如果 v 的 Kind 不是 struct ,它會(huì)發(fā)生混亂。
func (v Value) FieldByName(name string) Value
FieldByName 返回給定名稱的 struct 字段。如果沒有找到字段,它將返回零值。如果 v 的 Kind 不是 struct ,它會(huì)發(fā)生混亂。
func (v Value) FieldByNameFunc(match func(string) bool) Value
FieldByNameFunc 返回結(jié)構(gòu)字段,其名稱滿足匹配函數(shù)。如果 v 的 Kind 不是 struct ,它會(huì)發(fā)生混亂。如果沒有找到字段,它將返回零值。
func (v Value) Float() float64
Float 返回 v 的基礎(chǔ)值,作為 float64 。如果 v 的 Kind 不是 Float32 或 Float64 ,它會(huì)發(fā)生混亂
func (v Value) Index(i int) Value
索引返回 v 的第 i 個(gè)元素。如果 v 的 Kind 不是 Array,Slice 或 String ,或者我超出范圍,它會(huì)發(fā)生混亂。
func (v Value) Int() int64
Int 返回 v 的基礎(chǔ)值,如 int64 。如果 v 的 Kind 不是 Int , Int8 , Int16 , Int32 或 Int64 ,則會(huì)發(fā)生混亂。
func (v Value) Interface() (i interface{})
接口返回 v 的當(dāng)前值作為 interface{} 。它相當(dāng)于:
var i interface{} = (v's underlying value)
如果通過訪問未導(dǎo)出的結(jié)構(gòu)體域來獲取值,它會(huì)發(fā)生混亂。
func (v Value) InterfaceData() [2]uintptr
InterfaceData 以 uintptr 對(duì)的形式返回接口 v 的值。如果 v 的 Kind 不是 Interface ,它會(huì)發(fā)生混亂。
func (v Value) IsNil() bool
IsNil 報(bào)告其參數(shù) v 是否為零。參數(shù)必須是 chan , func ,接口,映射,指針或切片值; 如果不是,IsNil 恐慌。請(qǐng)注意,IsNil 并非總是與 Go 中的零比較。例如,如果通過使用未初始化的接口變量 i 調(diào)用 ValueOf 來創(chuàng)建 v ,那么 i == nil 將為 true,但 v.IsNil 會(huì)因?yàn)?v 將是零值而發(fā)生混亂。
func (v Value) IsValid() bool
IsValid 報(bào)告 v 是否表示一個(gè)值。如果 v 是零值,它將返回 false 。如果 IsValid 返回 false ,除了 String panic 之外的所有其他方法。大多數(shù)函數(shù)和方法永遠(yuǎn)不會(huì)返回?zé)o效值。如果有的話,其文件明確說明條件。
func (v Value) Kind() Kind
Kind返回v的Kind。如果v是零值(IsValid返回false),Kind將返回?zé)o效。
func (v Value) Len() int
Len 返回 v 的長度。如果 v 的 Kind 不是 Array,Chan,Map,Slice 或 String,它會(huì)發(fā)生混亂。
func (v Value) MapIndex(key Value) Value
MapIndex 返回地圖 v 中與鍵相關(guān)的值。如果 v 的 Kind 不是 Map ,它會(huì)發(fā)生混亂。如果在地圖中未找到鍵或者如果 v 表示零映射,它將返回零值。和 Go 一樣,鍵的值必須可以分配給地圖的鍵類型。
func (v Value) MapKeys() []Value
MapKeys 以未指定的順序返回包含地圖中所有鍵的切片。如果 v 的 Kind 不是 Map ,它會(huì)驚慌失措。如果 v 表示一個(gè)零映射,它將返回一個(gè)空片段。
func (v Value) Method(i int) Value
方法返回對(duì)應(yīng)于 v 的第 i 個(gè)方法的函數(shù)值。對(duì)返回函數(shù)調(diào)用的參數(shù)不應(yīng)包含接收方; 返回的函數(shù)將始終使用v作為接收者。如果我超出范圍或者 v 是一個(gè)無界面值,方法會(huì)發(fā)生混亂。
func (v Value) MethodByName(name string) Value
MethodByName 返回與給定名稱的v的方法對(duì)應(yīng)的函數(shù)值。對(duì)返回函數(shù)調(diào)用的參數(shù)不應(yīng)包含接收方; 返回的函數(shù)將始終使用 v 作為接收者。如果沒有找到方法,它返回零值。
func (v Value) NumField() int
NumField 返回 struct v 中的字段數(shù)。如果 v 的 Kind 不是 Struct ,則會(huì)發(fā)生混亂。
func (v Value) NumMethod() int
NumMethod 返回值的方法集中導(dǎo)出方法的數(shù)量。
func (v Value) OverflowComplex(x complex128) bool
OverflowComplex 報(bào)告 complex128 x 是否不能用 v 的類型表示。如果 v 的 Kind 不是 Complex64 或 Complex128,它會(huì)發(fā)生混亂。
func (v Value) OverflowFloat(x float64) bool
OverflowFloat 報(bào)告 float64 x 是否不能由 v 的類型表示。如果 v 的 Kind 不是 Float32 或 Float64 ,它會(huì)發(fā)生混亂。
func (v Value) OverflowInt(x int64) bool
OverflowInt 報(bào)告 int64 x 是否不能用 v 的類型表示。如果 v 的 Kind 不是 Int,Int8,int16,Int32 或 Int64,它會(huì)發(fā)生混亂。
func (v Value) OverflowUint(x uint64) bool
OverflowUint 報(bào)告 uint64 x 是否不能用 v 的類型表示。如果 v 的 Kind 不是 Uint,Uintptr,Uint8,Uint16,Uint32 或 Uint64,它會(huì)發(fā)生混亂。
func (v Value) Pointer() uintptr
指針返回 v 的值作為 uintptr 。它返回 uintptr 而不是 unsafe.Pointer,以便使用 reflect 的代碼無法顯式地導(dǎo)入不安全的包而無法獲取不安全的指針。如果 v 的 Kind 不是 Chan,F(xiàn)unc,Map,Ptr,Slice 或 UnsafePointer,它會(huì)發(fā)生混亂。
如果 v 的 Kind 是 Func,則返回的指針是底層代碼指針,但不一定足以唯一地標(biāo)識(shí)單個(gè)函數(shù)。唯一的保證是,當(dāng)且僅當(dāng) v 是一個(gè) nil func 值時(shí),結(jié)果為零。
如果 v 的種類是切片,返回的指針是切片的第一個(gè)元素。如果切片為零,則返回值為0.如果切片為空,但非零,則返回值不為零。
func (v Value) Recv() (x Value, ok bool)
Recv 接收并返回來自頻道 v 的值。如果 v 的 Kind 不是 Chan,它會(huì)發(fā)生混亂。接收阻塞,直到值準(zhǔn)備就緒。如果值 x 對(duì)應(yīng)于通道上的發(fā)送,則布爾值 ok;如果由于通道已關(guān)閉而接收到零值,則返回 false 。
func (v Value) Send(x Value)
發(fā)送在通道 v 上發(fā)送 x ,如果 v 的類型不是 Chan,或者 x 的類型與 v 的元素類型不是同一類型,則會(huì)發(fā)生混亂。和 Go 一樣,x 的值必須可分配給通道的元素類型。
func (v Value) Set(x Value)
將 x 賦值給 v 。如果 CanSet 返回 false,則會(huì)發(fā)生混亂。和 Go 一樣, x 的值必須賦值給 v 的類型。
func (v Value) SetBool(x bool)
SetBool 設(shè)置 v 的基礎(chǔ)值。如果 v 的 Kind 不是 Bool 或者 CanSet() 是 false ,它會(huì)發(fā)生混亂。
func (v Value) SetBytes(x []byte)
SetBytes 設(shè)置 v 的基礎(chǔ)值。如果 v 的基礎(chǔ)值不是一個(gè)字節(jié)片段,它會(huì)發(fā)生混亂。
func (v Value) SetCap(n int)
SetCap 將 v 的容量設(shè)置為 n 。如果 v 的 Kind 不是 Slice,或者 n 小于長度或大于切片的容量,它會(huì)發(fā)生混亂。
func (v Value) SetComplex(x complex128)
SetComplex 將 v 的基礎(chǔ)值設(shè)置為 x 。如果 v 的 Kind 不是 Complex64 或 Complex128 ,或者 CanSet() 為 false ,則會(huì)發(fā)生混亂。
func (v Value) SetFloat(x float64)
SetFloat 將 v 的基礎(chǔ)值設(shè)置為 x 。如果 v 的 Kind 不是 Float32 或 Float64 ,或者 CanSet() 是 false ,它會(huì)發(fā)生混亂。
func (v Value) SetInt(x int64)
SetInt 將 v 的基礎(chǔ)值設(shè)置為 x 。如果 v 的 Kind 不是 Int,Int8,Int16,Int32 或 Int64,或者 CanSet() 為 false,它會(huì)發(fā)生混亂。
func (v Value) SetLen(n int)
SetLen 將 v 的長度設(shè)置為 n 。如果 v 的 Kind 不是 Slice 或 n 是負(fù)值或大于切片的容量,它會(huì)發(fā)生混亂。
func (v Value) SetMapIndex(key, val Value)
SetMapIndex 將與地圖 v 中的鍵關(guān)聯(lián)的值設(shè)置為 val 。如果 v 的 Kind 不是 Map ,它會(huì)驚慌失措。如果 val 是零值,則 SetMapIndex 從地圖中刪除鍵。否則,如果 v 擁有一個(gè)零映射, SetMapIndex 將會(huì)出現(xiàn)混亂。與 Go 中一樣,鍵的值必須可以分配給地圖的鍵類型,并且 val 的值必須可以分配給地圖的值類型。
func (v Value) SetPointer(x unsafe.Pointer)
SetPointer 將 unsafe.Pointer 值 v 設(shè)置為 x 。如果 v 的 Kind 不是 UnsafePointer ,它會(huì)發(fā)生混亂。
func (v Value) SetString(x string)
SetString 將 v 的基礎(chǔ)值設(shè)置為 x 。如果 v 的 Kind 不是 String 或者 CanSet() 為 false ,它會(huì)發(fā)生混亂。
func (v Value) SetUint(x uint64)
SetUint 將 v 的基礎(chǔ)值設(shè)置為 x 。如果 v 的 Kind 不是 Uint,Uintptr,Uint8,Uint16,Uint32 或 Uint64,或者 CanSet() 為 false,它會(huì)發(fā)生混亂。
func (v Value) Slice(i, j int) Value
切片返回 vi:j 。如果 v 的 Kind 不是 Array,Slice 或 String,或者如果 v 是不可尋址的數(shù)組,或者索引超出范圍,它會(huì)發(fā)生混亂。
func (v Value) Slice3(i, j, k int) Value
Slice3 是 slice 操作的3索引形式:它返回 vi:j:k 。如果 v 的 Kind 不是 Array 或 Slice ,或者 v 是不可尋址的數(shù)組,或者索引超出范圍,它會(huì)發(fā)生混亂。
func (v Value) String() string
字符串以字符串形式返回字符串v的基礎(chǔ)值。由于 Go 的 String 方法約定,字符串是一種特殊情況。與其他 getter 不同,如果 v 的 Kind 不是 String,它不會(huì)驚慌。相反,它返回一個(gè)形式為“<T value>”的字符串,其中 T 是 v 的類型。fmt 包專門處理值。它不隱式地調(diào)用它們的 String 方法,而是打印它們保存的具體值。
func (v Value) TryRecv() (x Value, ok bool)
TryRecv 嘗試從通道 v 接收一個(gè)值,但不會(huì)阻塞。如果 v 的 Kind 不是 Chan ,它會(huì)驚慌失措。如果接收提供了一個(gè)值,x 是傳輸值,ok 是真實(shí)的。如果接收不能沒有阻塞地完成,則 x 是零值并且 ok 是假的。如果通道關(guān)閉,x 是通道元素類型的零值,ok 是假。
func (v Value) TrySend(x Value) bool
TrySend 嘗試在通道 v 上發(fā)送 x,但不會(huì)阻止。如果 v 的 Kind 不是 Chan ,它會(huì)驚慌失措。它報(bào)告是否發(fā)送了該值。和 Go 一樣, x 的值必須可分配給通道的元素類型。
func (v Value) Type() Type
類型返回 v 的類型。
func (v Value) Uint() uint64
Uint 返回 v 的基礎(chǔ)值,作為 uint64 。如果 v 的 Kind 不是 Uint,Uintptr,Uint8,Uint16,Uint32 或 Uint64,它會(huì)發(fā)生混亂。
func (v Value) UnsafeAddr() uintptr
UnsafeAddr 返回一個(gè)指向 v 數(shù)據(jù)的指針。對(duì)于那些也導(dǎo)入“不安全”軟件包的高級(jí)客戶來說。如果 v 不可尋址,它會(huì)發(fā)生混亂。
如果在不支持 Value 的 Value 上調(diào)用 Value 方法,則會(huì)發(fā)生 ValueError 。這些情況記錄在每種方法的描述中。
type ValueError struct { Method string Kind Kind}
func (e *ValueError) Error() string
? 如果名字相同,F(xiàn)ieldByName 和相關(guān)函數(shù)認(rèn)為結(jié)構(gòu)字段名稱是相等的,即使它們是源自不同包中的未導(dǎo)出名稱。這樣做的實(shí)際效果是,如果結(jié)構(gòu)類型t包含多個(gè)名為x的字段(從不同的包中嵌入),則 t.FieldByName(“x”) 的結(jié)果沒有很好地定義。FieldByName 可能會(huì)返回其中一個(gè)名為x的字段,或者可能會(huì)報(bào)告沒有字段。有關(guān)更多詳細(xì)信息,請(qǐng)參閱 golang.org/issue/4876 。