reflect
49 分钟阅读
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.
reflect
包实现了运行时反射,允许程序操作任意类型的对象。典型用法是将静态类型为interface{}
的值传递给TypeOf
函数提取其动态类型信息,TypeOf
函数返回一个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.
调用ValueOf
函数返回一个Value
类型的值,表示运行时数据。Zero
函数接受一个Type
参数,并返回表示该类型零值的Value。
See “The Laws of Reflection” for an introduction to reflection in Go: https://golang.org/doc/articles/laws_of_reflection.html
请参阅《反射法则》(The Laws of Reflection)了解Go语言中的反射介绍。
常量
|
|
Ptr is the old name for the Pointer kind.
Ptr
是Pointer
种类的旧名称。
变量
This section is empty.
函数
func Copy
|
|
Copy 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. Dst and src each must have kind Slice or Array, and dst and src must have the same element type.
Copy
函数将 src
的内容复制到 dst
,直到 dst
已满或 src
已耗尽。它返回已复制的元素数量。dst
和 src
必须都是 Slice
或 Array
类型,而且它们的元素类型必须相同。
As a special case, src can have kind String if the element type of dst is kind Uint8.
作为特殊情况,如果 dst
的元素类型是 Uint8
,则 src
(的类型,非元素的类型)可以是 String
类型。
|
|
func DeepEqual
|
|
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.
DeepEqual
函数报告 x
和 y
是否“深度相等(deeply equal)”,定义如下。两个具有相同类型的值在以下情况下被认为是深度相等。不同类型的值永远不会深度相等。
Array values are deeply equal when their corresponding elements are deeply equal.
Array
值在它们对应的元素深度相等时是深度相等的。
Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.
Struct
值在它们对应的字段(包括导出的和未导出的)深度相等时是深度相等的。
Func values are deeply equal if both are nil; otherwise they are not deeply equal.
如果两个Func
值都是nil
,则它们是深度相等的;否则,它们不是深度相等的。
Interface values are deeply equal if they hold deeply equal concrete values.
如果 Interface
值持有深度相等的具体值,那么它们是深度相等的。
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.
当以下所有条件都满足时,Map
值是深度相等的:它们都是nil
或者都不是nil
,它们有相同的长度,要么它们是相同的映射对象,要么它们相应的键(使用Go的相等性匹配)映射到深度相等的值。
Pointer values are deeply equal if they are equal using Go’s == operator or if they point to deeply equal values.
当它们使用 Go 的==
运算符相等,或者它们指向深度相等的值时,Pointer
值是深度相等的。
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.
当以下所有条件都满足时,Slice
值是深度相等的:它们都是nil
或者都不是nil
,它们有相同的长度,要么它们指向相同底层数组的相同初始条目(即,&x[0] == &y[0]
),要么它们相应的元素(达到长度)是深度相等的。请注意,非nil
的空切片和nil
切片(例如,[]byte{}
和[]byte(nil)
)不是深度相等的。
Other values - numbers, bools, strings, and channels - are deeply equal if they are equal using Go’s == operator.
其他值,如数字、布尔值、字符串和通道,如果它们使用Go的==
操作符相等,则是深度相等的。
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.
一般来说,DeepEqual
是Go的==
操作符的递归放宽版本。然而,如果不存在一些不一致性,这个想法是无法实现的。具体来说,一个值可能会与自己不相等,要么是因为它是函数类型(通常无法比较),要么是因为它是浮点NaN
值(在浮点比较中与自己不相等),要么是因为它是包含这样的值的数组、结构体或接口。另一方面,指针值总是与自己相等,即使它们指向或包含这样有问题的值,因为它们使用Go的==
操作符进行比较是相等的,这是一个足够的条件,使其无论内容如何都被视为深度相等。DeepEqual
的定义使得相同的捷径适用于切片和映射:如果x
和y
是相同的切片或相同的映射,则无论内容如何,它们都是深度相等的。
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.
DeepEqual
在遍历数据值时,可能会发现一个循环。DeepEqual
比较两个之前已经比较过的指针值时,第二次及以后会将这些值视为相等,而不是检查它们所指向的值。这确保了DeepEqual
能终止运行。
func Swapper <- go1.8
|
|
Swapper returns a function that swaps the elements in the provided slice.
Swapper
函数返回一个用于交换提供的切片中元素的函数。
Swapper panics if the provided interface is not a slice.
如果提供的接口不是切片类型,Swapper
函数会引发 panic。
类型
type ChanDir
|
|
ChanDir represents a channel type’s direction.
ChanDir
类型表示通道类型的方向。
|
|
(ChanDir) String
|
|
String
方法返回 ChanDir
的字符串形式。
type Kind
|
|
A Kind represents the specific kind of type that a Type represents. The zero Kind is not a valid kind.
Kind
类型表示 Type
表示的特定类型。零 Kind
不是有效的类型。
Kind Example
|
|
|
|
(Kind) String
|
|
String returns the name of k.
String
方法返回k
的名称。
type MapIter <- go1.12
|
|
A MapIter is an iterator for ranging over a map. See Value.MapRange.
MapIter
是用于遍历映射的迭代器。参见 Value.MapRange。
(*MapIter) Key <- go1.12
|
|
Key returns the key of iter’s current map entry.
Key
方法返回iter
当前映射条目的键。
(*MapIter) Next <- go1.12
|
|
Next advances the map iterator and reports whether there is another entry. It returns false when iter is exhausted; subsequent calls to Key, Value, or Next will panic.
Next
方法推进映射迭代器,并报告是否有另一个条目。当iter
耗尽时,它返回false
;对Key
方法,Value
方法或Next
方法的后续调用将引发panic。
(*MapIter) Reset <- go1.18
|
|
Reset modifies iter to iterate over v. It panics if v’s Kind is not Map and v is not the zero Value. Reset(Value{}) causes iter to not to refer to any map, which may allow the previously iterated-over map to be garbage collected.
Reset
方法修改iter
以遍历v
。如果v
的Kind
不是Map
且v
不是零Value
,则它会引发panic。Reset(Value{})
会导致iter
不引用任何映射,这可能允许之前遍历过的映射被垃圾回收。
(*MapIter) Value <- go1.12
|
|
Value returns the value of iter’s current map entry.
Value
方法返回iter
当前映射条目的值。
type Method
|
|
Method represents a single method.
Method
表示单个方法。
(Method) IsExported <- go1.17
|
|
IsExported reports whether the method is exported.
IsExported
方法返回该方法是否为导出方法。
type SelectCase <- go1.1
|
|
A SelectCase describes a single case in a select operation. The kind of case depends on Dir, the communication direction.
SelectCase
描述 select
操作中的一个单独 case
。情况的种类取决于 Dir
,通信方向。
If Dir is SelectDefault, the case represents a default case. Chan and Send must be zero Values.
如果 Dir
是 SelectDefault
,则该 case
表示默认 case。Chan
和 Send
字段必须是零 Value
。
If Dir is SelectSend, the case represents a send operation. Normally Chan’s underlying value must be a channel, and Send’s underlying value must be assignable to the channel’s element type. As a special case, if Chan is a zero Value, then the case is ignored, and the field Send will also be ignored and may be either zero or non-zero.
如果 Dir
是 SelectSend
,则该 case
表示发送操作。通常情况下,Chan
字段的底层值必须是通道,并且 Send
字段的底层值必须可以赋值给通道的元素类型。作为特殊情况,如果 Chan
字段是零 Value
,则 case
将被忽略,Send
字段也将被忽略,可以是零或非零。
If Dir is SelectRecv, the case represents a receive operation. Normally Chan’s underlying value must be a channel and Send must be a zero Value. If Chan is a zero Value, then the case is ignored, but Send must still be a zero Value. When a receive operation is selected, the received Value is returned by Select.
如果 Dir
是 SelectRecv
,则该 case
表示接收操作。通常情况下,Chan
字段的底层值必须是通道,Send
字段必须是零 Value
。如果 Chan
是零 Value
,则 case
将被忽略,但 Send
仍然必须是零 Value
。当选择接收操作时,接收到的 Value
将由 Select
返回。
type SelectDir <- go1.1
|
|
A SelectDir describes the communication direction of a select case.
SelectDir
描述 select
case
的通信方向。
|
|
type SliceHeader
|
|
SliceHeader is the runtime representation of a slice. It cannot be used safely or portably and its representation may change in a later release. Moreover, the Data field is not sufficient to guarantee the data it references will not be garbage collected, so programs must keep a separate, correctly typed pointer to the underlying data.
SliceHeader
是切片的运行时表示。它不能安全或可移植地使用,其表示可能会在以后的版本中更改。此外,Data
字段不足以保证其引用的数据不会被垃圾收集,因此程序必须保留一个单独的、正确类型的指针来引用底层数据。
In new code, use unsafe.Slice or unsafe.SliceData instead.
在新代码中,请使用 unsafe.Slice
或 unsafe.SliceData
代替。
type StringHeader
|
|
StringHeader is the runtime representation of a string. It cannot be used safely or portably and its representation may change in a later release. Moreover, the Data field is not sufficient to guarantee the data it references will not be garbage collected, so programs must keep a separate, correctly typed pointer to the underlying data.
StringHeader
是字符串的运行时表示。它不能安全或可移植地使用,其表示可能会在以后的版本中更改。此外,Data
字段不足以保证其引用的数据不会被垃圾收集,因此程序必须保留一个单独的、正确类型的指针来引用底层数据。
In new code, use unsafe.String or unsafe.StringData instead.
在新代码中,请使用 unsafe.String
或 unsafe.StringData
代替。
type StructField
|
|
A StructField describes a single field in a struct.
StructField
描述结构体中的一个字段。
func VisibleFields <- go1.17
|
|
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.
VisibleFields
函数返回类型 t
中的所有可见字段,t
必须是结构体类型。字段定义为可直接通过 FieldByName
方法调用访问的字段。返回的字段包括嵌入结构体成员内的字段和未导出字段。它们遵循与结构体中找到的相同顺序,嵌入字段后面紧跟其被提升的字段。
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).
对于返回的切片的每个元素 e
,可以通过调用 v.FieldByIndex(e.Index)
从类型 t
的值 v
中获取相应的字段。
(StructField) IsExported <- go1.17
|
|
IsExported reports whether the field is exported.
IsExported
方法报告字段是否已导出。
type StructTag
|
|
A StructTag is the tag string in a struct field.
StructTag
是结构体字段中的标签字符串。
By convention, tag strings are a concatenation of optionally space-separated key:“value” pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ’ ‘), quote (U+0022 ‘"’), and colon (U+003A ‘:’). Each value is quoted using U+0022 ‘"’ characters and Go string literal syntax.
按照约定,标记字符串是一个可选的空格分隔的key:"value"
对的串联。每个key
都是一个由非空格(U+0020 ’ ‘)、引号(U+0022 ‘"
’)和冒号(U+003A ‘:
’)之外的控制字符组成的非空字符串。每个value
都使用U+0022 ‘"
‘字符和Go字符串文字语法引起引用。
StructTag Example
|
|
(StructTag) Get
|
|
Get returns the value associated with key in the tag string. If there is no such key in the tag, Get returns the empty string. If the tag does not have the conventional format, the value returned by Get is unspecified. To determine whether a tag is explicitly set to the empty string, use Lookup.
Get
方法在标签字符串中返回与key
关联的值。如果标签中没有这样的键,则 Get
返回空字符串。如果标签不具有常规格式,则 Get
返回的值是未指定的。要确定标签是否显式设置为空字符串,请使用 Lookup
方法。
(StructTag) Lookup <- go1.7
|
|
Lookup returns the value associated with key in the tag string. If the key is present in the tag the value (which may be empty) is returned. Otherwise the returned value will be the empty string. The ok return value reports whether the value was explicitly set in the tag string. If the tag does not have the conventional format, the value returned by Lookup is unspecified.
Lookup
方法在标签字符串中返回与key
关联的值。如果标签中存在该键,则返回值(可能为空)。否则,返回的值将是空字符串。ok
返回值报告值是否在标签字符串中显式设置。如果标签不具有常规格式,则 Lookup
返回的值是未指定的。
Lookup Example
|
|
type Type
|
|
Type is the representation of a Go type.
Type
是 Go 语言类型的表示。
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.
并非所有的方法都适用于所有类型。如果有任何限制,会在每个方法的文档中注明。在调用特定类型的方法之前,请使用 Kind
方法来查明类型的种类。如果调用的方法是不适合类型的种类,会导致运行时恐慌(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.
Type
值是可比较的,例如使用 ==
运算符,因此它们可以用作映射键。如果两个 Type
值表示相同的类型,则它们是相等的。
func ArrayOf <- go1.5
|
|
ArrayOf returns the array type with the given length and element type. For example, if t represents int, ArrayOf(5, t) represents [5]int.
ArrayOf
函数返回给定长度和元素类型的数组类型。例如,如果 t
代表 int
,则 ArrayOf(5, t)
代表 [5]int
。
If the resulting type would be larger than the available address space, ArrayOf panics.
如果结果类型大于可用的地址空间,ArrayOf
会引发恐慌。
func ChanOf <- go1.1
|
|
ChanOf returns the channel type with the given direction and element type. For example, if t represents int, ChanOf(RecvDir, t) represents <-chan int.
ChanOf
函数返回给定方向和元素类型的通道类型。例如,如果 t
代表 int
,则 ChanOf(RecvDir, t)
代表 <-chan int
。
The gc runtime imposes a limit of 64 kB on channel element types. If t’s size is equal to or exceeds this limit, ChanOf panics.
gc 运行时对通道元素类型的大小限制为 64 kB。如果 t
的大小等于或超过此限制,ChanOf
会引发恐慌。
func FuncOf <- go1.5
|
|
FuncOf returns the function type with the given argument and result types. For example if k represents int and e represents string, FuncOf([]Type{k}, []Type{e}, false) represents func(int) string.
FuncOf
函数返回给定实参和结果类型的函数类型。例如,如果 k
代表 int
,e
代表 string
,则 FuncOf([]Type{k}, []Type{e}, false)
代表 func(int) string
。
The variadic argument controls whether the function is variadic. FuncOf panics if the in[len(in)-1] does not represent a slice and variadic is true.
变参实参控制函数是否为变参函数。如果 in[len(in)-1]
不表示一个切片且 variadic
为 true
,FuncOf
会引发恐慌。
func MapOf <- go1.1
|
|
MapOf returns the map type with the given key and element types. For example, if k represents int and e represents string, MapOf(k, e) represents map[int]string.
MapOf
函数返回给定键和元素类型的映射类型。例如,如果 k
代表 int
,e
代表 string
,则 MapOf(k, e)
代表 map[int]string
。
If the key type is not a valid map key type (that is, if it does not implement Go’s == operator), MapOf panics.
如果键类型不是有效的映射键类型(即,如果它没有实现 Go 的 ==
操作符),MapOf
会引发恐慌。
func PointerTo <- go1.18
|
|
PointerTo returns the pointer type with element t. For example, if t represents type Foo, PointerTo(t) represents *Foo.
PointerTo
函数返回元素为 t
的指针类型。例如,如果 t
代表 Foo
类型,PointerTo(t)
就代表 *Foo
。
func PtrTo
|
|
PtrTo returns the pointer type with element t. For example, if t represents type Foo, PtrTo(t) represents *Foo.
PtrTo
函数返回元素为 t
的指针类型。例如,如果 t
代表 Foo
类型,PtrTo(t)
就代表 *Foo
。
PtrTo is the old spelling of PointerTo. The two functions behave identically.
PtrTo
是 PointerTo
的旧拼写方式。这两个函数的行为完全相同。
func SliceOf <- go1.1
|
|
SliceOf returns the slice type with element type t. For example, if t represents int, SliceOf(t) represents []int.
SliceOf
函数返回具有元素类型t
的切片类型。例如,如果t
表示int
,则SliceOf(t)
表示[]int
。
func StructOf <- go1.7
|
|
StructOf returns the struct type containing fields. The Offset and Index fields are ignored and computed as they would be by the compiler.
StructOf
函数返回包含 fields
的结构体类型。Offset
和 Index
字段会被忽略,并会计算出编译器所期望的值。
StructOf currently does not generate wrapper methods for embedded fields and panics if passed unexported StructFields. These limitations may be lifted in a future version.
StructOf
目前不会为嵌入字段生成包装方法,如果传入未导出的 StructFields
,它会引发恐慌。这些限制在未来的版本中可能会被解除。
StructOf Example
|
|
func TypeFor <-go1.22.0
|
|
TypeFor returns the Type that represents the type argument T.
TypeFor
函数返回表示类型实参T
的Type。
func TypeOf
|
|
TypeOf returns the reflection Type that represents the dynamic type of i. If i is a nil interface value, TypeOf returns nil.
TypeOf
函数返回表示 i
的动态类型的反射 Type
。如果 i
是 nil
接口值,TypeOf
返回 nil
。
typeOf Example
|
|
type Value
|
|
Value is the reflection interface to a Go value.
Value
是Go值的反射接口。
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.
并非所有方法都适用于所有类型的值。如果有任何限制,会在每个方法的文档中注明。在调用特定类型的方法之前,使用Kind方法来查明值的类型。调用不适合该类型的方法会导致运行时恐慌。
The zero Value represents no value. Its IsValid method returns false, its Kind method returns Invalid, its String method returns “
零值Value代表没有值。其IsValid
方法返回false
,其Kind
方法返回Invalid
,其String
方法返回"<invalid Value>
",所有其他方法都会引发恐慌。大多数函数和方法永远不会返回无效的值。如果有,其文档会明确地说明这些条件。
A Value can be used concurrently by multiple goroutines provided that the underlying Go value can be used concurrently for the equivalent direct operations.
如果基础的Go值可以被并发地用于等价的直接操作,那么一个Value
可以被多个goroutine并发地使用。
To compare two Values, compare the results of the Interface method. Using == on two Values does not compare the underlying values they represent.
要比较两个Value
,请比较它们的Interface
方法的结果。在两个Value
上使用==
并不会比较它们所代表的基础值。
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.
Append
函数将值x
追加到切片s
上,并返回结果切片。像在Go中一样,每个x
的值必须可以分配给切片的元素类型。
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.
AppendSlice
函数将切片t
追加到切片s
上,并返回结果切片。切片s
和t
必须具有相同的元素类型。
func Indirect
|
|
Indirect returns the value that v points to. If v is a nil pointer, Indirect returns a zero Value. If v is not a pointer, Indirect returns v.
Indirect
函数返回v
指向的值。如果v
是一个nil
指针,Indirect
返回一个零值。如果v
不是一个指针,Indirect
返回v
。
func MakeChan
|
|
MakeChan creates a new channel with the specified type and buffer size.
MakeChan
函数使用指定的类型和缓冲区大小创建一个新的通道。
func MakeFunc <- go1.1
|
|
MakeFunc returns a new function of the given Type that wraps the function fn. When called, that new function does the following:
MakeFunc
返回一个给定Type
的新函数,该函数包装了fn
。当被调用时,这个新函数执行以下操作:
- converts its arguments to a slice of Values.
- 将其实参转换为
Value
切片。 - runs results := fn(args).
- 运行
results := fn(args)
。 - returns the results as a slice of Values, one per formal result.
- 将结果作为
Value
切片返回,每个形式结果一个。
The implementation fn can assume that the argument Value slice has the number and type of arguments given by typ. If typ describes a variadic function, the final Value is itself a slice representing the variadic arguments, as in the body of a variadic function. The result Value slice returned by fn must have the number and type of results given by typ.
fn
实现可以假定实参Value
切片具有由 typ
给出的实参的数量和类型。如果 typ
描述了一个可变参数函数,那么最后一个 Value
本身就是一个代表可变实参的切片,就像在可变参数函数的主体中一样。由 fn
返回的结果 Value
切片必须具有由 typ
给出的结果的数量和类型。
The Value.Call method allows the caller to invoke a typed function in terms of Values; in contrast, MakeFunc allows the caller to implement a typed function in terms of Values.
Value.Call
方法允许调用者根据 Value
来调用类型化的函数;与此相反,MakeFunc
允许调用者根据 Value
来实现类型化的函数。
The Examples section of the documentation includes an illustration of how to use MakeFunc to build a swap function for different types.
文档中的Examples 部分包括一个关于如何使用 MakeFunc
为不同类型构建交换函数的说明。
MakeFunc Example
|
|
func MakeMap
|
|
MakeMap creates a new map with the specified type.
MakeMap
函数使用指定的类型创建一个新的映射。
func MakeMapWithSize <- go1.9
|
|
MakeMapWithSize creates a new map with the specified type and initial space for approximately n elements.
MakeMapWithSize
函数使用一个指定的类型和大约n
个元素的初始空间创建一个新的映射。
func MakeSlice
|
|
MakeSlice creates a new zero-initialized slice value for the specified slice type, length, and capacity.
MakeSlice
函数为指定的切片类型、长度和容量创建一个新的零值初始化切片值。
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 PointerTo(typ).
New
函数返回一个 Value
,它代表指定类型的新零值的指针。也就是说,返回的 Value
的 Type
是 PointerTo(typ)
。
func NewAt
|
|
NewAt returns a Value representing a pointer to a value of the specified type, using p as that pointer.
NewAt
函数返回一个 Value
,它表示指向指定类型值的指针,使用 p
作为该指针。
func Select <- go1.1
|
|
Select executes a select operation described by the list of cases. Like the Go select statement, it blocks until at least one of the cases can proceed, makes a uniform pseudo-random choice, and then executes that case. It returns the index of the chosen case and, if that case was a receive operation, the value received and a boolean indicating whether the value corresponds to a send on the channel (as opposed to a zero value received because the channel is closed). Select supports a maximum of 65536 cases.
Select
函数执行由 cases
列表描述的 select
操作。与 Go 的 select
语句类似,它会阻塞,直到至少有一个 case
可以进行,然后做出统一伪随机的(uniform pseudo-random)选择,并执行该 case
。它返回所选 case
的索引,如果该 case
是接收操作,则返回接收的值和一个布尔值,该布尔值表示该值是否与通道上的发送相对应(而不是因为通道关闭而接收到的零值)。Select
支持最多 65536
个 cases。
func SliceAt <- go1.23.0
|
|
SliceAt returns a Value representing a slice whose underlying data starts at p, with length and capacity equal to n.
SliceAt 返回一个 Value,表示一个底层数据从 p 开始的 slice,长度和容量等于 n。
This is like unsafe.Slice.
这类似于 unsafe.Slice。
func ValueOf
|
|
ValueOf returns a new Value initialized to the concrete value stored in the interface i. ValueOf(nil) returns the zero Value.
ValueOf
函数返回一个新的 Value
,初始化为接口 i
中存储的具体值。ValueOf(nil)
返回零值 Value
。
func Zero
|
|
Zero returns a Value representing the 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. The returned value is neither addressable nor settable.
Zero
函数返回一个表示指定类型的零值的 Value
。该结果与Value
结构体的零值不同,后者表示根本没有值。例如,Zero(TypeOf(42))
返回一个 Kind
为 Int
,值为 0
的 Value
。返回的值既不可寻址也不可设置。
(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.
Addr
返回表示v
的地址的指针值。如果CanAddr()
返回false
,它将引发panic
。Addr
通常用于获取结构体字段或切片元素的指针,以便调用需要指针接收者的方法。
(Value) Bool
|
|
Bool returns v’s underlying value. It panics if v’s kind is not Bool.
Bool
方法返回v
的基础值。如果v
的kind不是Bool
,它会引发panic。
(Value) Bytes
|
|
Bytes returns v’s underlying value. It panics if v’s underlying value is not a slice of bytes or an addressable array of bytes.
Bytes
方法返回v
的底层值。如果v
的底层值既不是字节的切片,也不是可寻址的字节数组,它会引发panic。
(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.
Call
方法使用输入实参in
调用函数v
。例如,如果len(in) == 3
,v.Call(in)
表示Go调用v(in[0], in[1], in[2])
。如果v
的Kind
不是Func
,Call
会引发panic。它以Value
的形式返回输出结果。像在Go中一样,每个输入实参必须可以赋值给函数相应输入参数的类型。如果v
是可变参数函数,Call
会自己创建可变参数切片,并复制相应的值。
(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.CallSlice(in) represents the Go call v(in[0], in[1], in[2]…). CallSlice 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.
CallSlice
使用输入实参in
调用可变参数函数v
,将切片in[len(in)-1]
赋值给v
的最后一个可变参数。例如,如果len(in) == 3
,v.CallSlice(in)
表示Go调用v(in[0], in[1], in[2]...)
。如果v
的Kind
不是Func
,或者v
不是可变参数,CallSlice
会引发panic。它以Value
的形式返回输出结果。像在Go中一样,每个输入实参必须可以赋值给函数相应输入参数的类型。
(Value) CanAddr
|
|
CanAddr reports whether 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.
CanAddr
方法报告是否可以使用 Addr
获取值的地址。这样的值被称为可寻址的。如果一个值是切片的一个元素、可寻址数组的一个元素、可寻址结构体的一个字段,或者是指针解引用的结果,那么它就是可寻址的。如果 CanAddr
返回 false
,调用 Addr
方法将会引发 panic。
(Value) CanComplex <- go1.18
|
|
CanComplex reports whether Complex can be used without panicking.
CanComplex
方法报告是否可以使用 Complex
方法而不引发 panic。
(Value) CanConvert <- go1.17
|
|
CanConvert reports whether the value v can be converted to type t. If v.CanConvert(t) returns true then v.Convert(t) will not panic.
CanConvert
方法报告值 v
是否可以被转换为类型 t
。如果 v.CanConvert(t)
返回 true
,那么 v.Convert(t)
将不会引发 panic。
(Value) CanFloat <- go1.18
|
|
CanFloat reports whether Float can be used without panicking.
CanFloat
方法报告是否可以使用 Float
方法而不引发 panic。
(Value) CanInt <- go1.18
|
|
CanInt reports whether Int can be used without panicking.
CanInt
方法报告是否可以使用 Int
方法而不引发 panic。
(Value) CanInterface
|
|
CanInterface reports whether Interface can be used without panicking.
CanInterface
方法报告是否可以使用 Interface
方法而不引发 panic。
(Value) CanSet
|
|
CanSet reports whether 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, SetInt) will panic.
CanSet
方法报告是否可以更改 v
的值。只有当它是可寻址的,并且不是通过未导出的结构体字段获得时,才能更改 Value
。如果 CanSet
返回 false
,调用 Set
方法或任何特定类型的设置器(例如,SetBool
方法、SetInt
方法)将会引发 panic。
(Value) CanUint <- go1.18
|
|
CanUint reports whether Uint can be used without panicking.
CanUint
方法报告是否可以使用 Uint
方法而不引发 panic。
(Value) Cap
|
|
Cap returns v’s capacity. It panics if v’s Kind is not Array, Chan, Slice or pointer to Array.
Cap
方法返回 v
的容量。如果 v
的 Kind
不是 Array
、Chan
、Slice
或 Array
的指针,它会引发 panic。
(Value) Clear <-go1.21.0
|
|
Clear clears the contents of a map or zeros the contents of a slice.
Clear 清空 map 的内容或将 slice 的内容置零。
It panics if v’s Kind is not Map or Slice.
如果 v 的 Kind 不是 Map 或 Slice,会触发 panic。
(Value) Close
|
|
Close closes the channel v. It panics if v’s Kind is not Chan.
Close
方法关闭通道 v
。如果 v
的 Kind
不是 Chan
,它会引发 panic。
(Value) Comparable <- go1.20
|
|
Comparable reports whether the value v is comparable. If the type of v is an interface, this checks the dynamic type. If this reports true then v.Interface() == x will not panic for any x, nor will v.Equal(u) for any Value u.
Comparable
方法报告值 v
是否可比较。如果 v
的类型是接口,则会检查动态类型。如果此报告为 true
,则 v.Interface() == x
不会对任何 x
引发 panic,对于任何 Value u
,v.Equal(u)
也不会引发 panic。
(Value) Complex
|
|
Complex returns v’s underlying value, as a complex128. It panics if v’s Kind is not Complex64 or Complex128.
Complex
方法返回 v
的底层值,作为 complex128
。如果 v
的 Kind
不是 Complex64
或 Complex128
,它会引发 panic。
(Value) Convert <- go1.1
|
|
Convert returns the value v converted to type t. If the usual Go conversion rules do not allow conversion of the value v to type t, or if converting v to type t panics, Convert panics.
Convert
方法返回将值 v
转换为类型 t
的结果。如果通常的 Go 转换规则不允许将值 v
转换为类型 t
,或者如果将 v
转换为类型 t
引发 panic,Convert
就会引发 panic。
(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 Pointer. It returns the zero Value if v is nil.
Elem
方法返回接口 v
包含的值或指针 v
指向的值。如果 v
的 Kind
不是 Interface
或 Pointer
,它会引发 panic。如果 v
是 nil
,它会返回零值 Value
。
(Value) Equal <- go1.20
|
|
Equal reports true if v is equal to u. For two invalid values, Equal will report true. For an interface value, Equal will compare the value within the interface. Otherwise, If the values have different types, Equal will report false. Otherwise, for arrays and structs Equal will compare each element in order, and report false if it finds non-equal elements. During all comparisons, if values of the same type are compared, and the type is not comparable, Equal will panic.
Equal
方法报告 v
是否等于 u
。对于两个无效值,Equal
将报告 true
。对于接口值,Equal
将比较接口中的值。否则,如果值的类型不同,Equal
将报告 false
。否则,对于数组和结构体,Equal
将按顺序比较每个元素,并在找到不相等的元素时报告 false
。在所有比较中,如果比较相同类型的值,并且类型不可比较,Equal
就会引发 panic。
(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.
Field
方法返回结构体 v
的第 i
个字段。如果 v
的 Kind
不是 Struct
或 i
越界,它会引发 panic。
(Value) FieldByIndex
|
|
FieldByIndex returns the nested field corresponding to index. It panics if evaluation requires stepping through a nil pointer or a field that is not a struct.
FieldByIndex
方法返回与index
相对应的嵌套字段。如果求值需要通过nil指针或不是结构体的字段,则会引发panic。
FieldByIndex Example
|
|
(Value) FieldByIndexErr <- go1.18
|
|
FieldByIndexErr returns the nested field corresponding to index. It returns an error if evaluation requires stepping through a nil pointer, but panics if it must step through a field that is not a struct.
FieldByIndexErr
方法返回与index
相对应的嵌套字段,并返回一个错误。如果求值需要通过nil
指针,则返回错误;如果必须通过不是结构体的字段,则会引发panic。
(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.
FieldByName
方法返回给定名称的结构体字段。如果没有找到字段,则返回零值。如果v
的Kind
不是Struct
,则会引发panic。
FieldByName Example
|
|
(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.
FieldByNameFunc
方法返回满足match
函数的名称的结构体字段。如果v
的Kind
不是Struct
,则会引发panic。如果没有找到字段,则返回零值。
(Value) Float
|
|
Float returns v’s underlying value, as a float64. It panics if v’s Kind is not Float32 or Float64.
Float
方法将v
的底层值作为float64
返回。如果v
的Kind
不是Float32
或Float64
,则会引发panic。
(Value) Grow <- go1.20
|
|
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.
Grow
方法增加切片的容量(如果需要),以保证有空间容纳另外n
个元素。在Grow(n)
之后,至少可以追加n
个元素到切片,而不需要再次分配内存。
It panics if v’s Kind is not a Slice or if n is negative or too large to allocate the memory.
如果v
的Kind
不是Slice
,或者n
是负数,或者太大以至于无法分配内存,则会引发panic。
(Value) Index
|
|
Index returns v’s i’th element. It panics if v’s Kind is not Array, Slice, or String or i is out of range.
Index
方法返回v
的第i
个元素。如果v
的Kind
不是Array
,Slice
或String
,或者i
越界,则会引发panic。
(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.
Int
方法将v
的底层值作为int64
返回。如果v
的Kind
不是Int
,Int8
,Int16
,Int32
或Int64
,则会引发panic。
(Value) Interface
|
|
Interface returns v’s current value as an interface{}. It is equivalent to:
Interface
方法将v
的当前值作为interface{}
返回。它等同于:
|
|
It panics if the Value was obtained by accessing unexported struct fields.
如果Value
是通过访问未导出的结构体字段获得的,则会引发panic。
(Value) InterfaceData <- DEPRECATED
|
|
InterfaceData returns a pair of unspecified uintptr values. It panics if v’s Kind is not Interface.
InterfaceData
方法返回一个未指定的uintptr值对。如果v
的Kind
不是Interface
,就会引发panic。
In earlier versions of Go, this function returned the interface’s value as a uintptr pair. As of Go 1.4, the implementation of interface values precludes any defined use of InterfaceData.
在Go的早期版本中,这个函数将接口的值作为uintptr对返回。然而,自Go 1.4
起,接口值的实现不再支持任何定义的InterfaceData
用法。
Deprecated: The memory representation of interface values is not compatible with InterfaceData.
已弃用:接口值的内存表示与InterfaceData
不兼容。
(Value) IsNil
|
|
IsNil reports whether its argument v is nil. The argument must be a chan, func, interface, map, pointer, or slice value; if it is not, IsNil panics. Note that IsNil is not always equivalent to a regular comparison with nil in Go. For example, if v was created by calling ValueOf with an uninitialized interface variable i, i==nil will be true but v.IsNil will panic as v will be the zero Value.
IsNil
方法报告其实参v
是否为nil
。实参必须是chan
、func
、interface
、map
、pointer
或slice
值;如果不是,IsNil
就会引发panic。请注意,IsNil
并不总是等同于Go中使用nil
的常规比较。例如,如果v
是通过使用未初始化的接口变量i
调用ValueOf
函数创建的,那么i==nil
将为true
,但v.IsNil
会引发panic,因为v
将是零值Value
。(参见以下个人给出的示例)
|
|
(Value) IsValid
|
|
IsValid reports whether 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.
IsValid
报告v
是否代表一个值。如果v
是零值,它会返回false
。如果IsValid
返回false
,除String
外的所有其他方法都会引发panic。大多数函数和方法永远不会返回一个无效的值。如果一个函数或方法确实返回了无效值,那么它的文档会明确地说明这些条件。
(Value) IsZero <- go1.13
|
|
IsZero reports whether v is the zero value for its type. It panics if the argument is invalid.
IsZero
方法报告v
是否为其类型的零值。如果实参无效,则会引发panic。
(Value) Kind
|
|
Kind returns v’s Kind. If v is the zero Value (IsValid returns false), Kind returns Invalid.
Kind
方法返回v
的Kind
。如果v
是零值(IsValid
返回false
),Kind
方法返回Invalid
。
(Value) Len
|
|
Len returns v’s length. It panics if v’s Kind is not Array, Chan, Map, Slice, String, or pointer to Array.
Len
方法返回v
的长度。如果v
的Kind
不是Array
、Chan
、Map
、Slice
、String
或者指向Array
的指针,它会引发panic。
(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.
MapIndex
方法返回在映射v
中与key
关联的值。如果v
的Kind
不是Map
,它会引发panic。如果在映射中找不到key
,或者v
代表一个nil
映射,它会返回零值。就像在Go中一样,key
的值必须可分配给映射的键类型。
(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.
MapKeys
方法返回一个包含映射中所有存在的key的切片,顺序未指定。如果v
的Kind
不是Map
,它会引发panic。如果v
代表一个nil
映射,它会返回一个空切片。
(Value) MapRange <- go1.12
|
|
MapRange returns a range iterator for a map. It panics if v’s Kind is not Map.
MapRange
方法返回一个映射的范围迭代器。如果v
的Kind
不是Map
,它会引发panic。
Call Next to advance the iterator, and Key/Value to access each entry. Next returns false when the iterator is exhausted. MapRange follows the same iteration semantics as a range statement.
调用Next
方法来推进迭代器,并调用Key
/Value
方法来访问每个条目。当迭代器耗尽时,Next
方法返回false
。MapRange
方法遵循与range
语句相同的迭代语义。
Example:
示例:
|
|
(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 or if v is a nil interface value.
Method
方法返回与v
的第i
个方法对应的函数值。对返回的函数进行Call
的实参不应包括接收器;返回的函数将始终使用v
作为接收器。如果i
超出范围,或者v
是一个nil
接口值,Method
会引发panic。
(Value) MethodByName
|
|
MethodByName returns a function value corresponding to the method of v with the given name. The arguments to a Call on the returned function should not include a receiver; the returned function will always use v as the receiver. It returns the zero Value if no method was found.
MethodByName
方法根据给定的名称返回与v
的方法相对应的函数值。对返回的函数进行Call
的实参不应包含接收器;返回的函数将始终使用v
作为接收器。如果没有找到对应的方法,它会返回零值。
(Value) NumField
|
|
NumField returns the number of fields in the struct v. It panics if v’s Kind is not Struct.
NumField
方法返回结构体v
中的字段数量。如果v
的Kind
不是Struct
,则会引发panic。
(Value) NumMethod
|
|
NumMethod returns the number of methods in the value’s method set.
NumMethod
方法返回值的方法集中的方法数量。
For a non-interface type, it returns the number of exported methods.
对于非接口类型,它返回导出的方法数量。
For an interface type, it returns the number of exported and unexported methods.
对于接口类型,它返回导出和未导出的方法数量。
(Value) OverflowComplex
|
|
OverflowComplex reports whether the complex128 x cannot be represented by v’s type. It panics if v’s Kind is not Complex64 or Complex128.
OverflowComplex
方法报告complex128
x
是否无法由v
的类型表示。如果v
的Kind 不是Complex64
或Complex128
,则会引发panic。
(Value) OverflowFloat
|
|
OverflowFloat reports whether the float64 x cannot be represented by v’s type. It panics if v’s Kind is not Float32 or Float64.
OverflowFloat
方法报告float64
x
是否无法由v
的类型表示。如果v
的Kind 不是Float32
或Float64
,则会引发panic。
(Value) OverflowInt
|
|
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.
OverflowInt
方法报告int64
x
是否无法由v
的类型表示。如果v
的Kind
不是Int
、Int8
、Int16
、Int32
或Int64
,则会引发panic。
(Value) OverflowUint
|
|
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.
OverflowUint
方法报告uint64
x
是否无法由v
的类型表示。如果v
的Kind
不是Uint
、Uintptr
、Uint8
、Uint16
、Uint32
或Uint64
,则会引发panic。
(Value) Pointer
|
|
Pointer returns v’s value as a uintptr. It panics if v’s Kind is not Chan, Func, Map, Pointer, Slice, or UnsafePointer.
Pointer
方法将v
的值作为uintptr
返回。如果v
的Kind
不是Chan
、Func
、Map
、Pointer
、Slice
或UnsafePointer
,它会引发panic。
If v’s Kind is Func, the returned pointer is an underlying code pointer, but not necessarily enough to identify a single function uniquely. The only guarantee is that the result is zero if and only if v is a nil func Value.
如果v
的Kind
是Func
,返回的指针是一个底层代码指针,但不一定足以唯一标识一个函数。唯一的保证是,当且仅当v
是nil func Value
时,结果为零。
If v’s Kind is Slice, the returned pointer is to the first element of the slice. If the slice is nil the returned value is 0. If the slice is empty but non-nil the return value is non-zero.
如果v
的Kind
是Slice
,返回的指针指向切片的第一个元素。如果切片是nil
,返回的值是0
。如果切片是空的但非nil
,返回值是非0。
It’s preferred to use uintptr(Value.UnsafePointer()) to get the equivalent result.
建议使用uintptr(Value.UnsafePointer())
来获得等价的结果。
(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.
Recv
方法从通道 v
中接收并返回一个值。如果 v
的 Kind
不是 Chan
,则会 panic。该接收操作会阻塞直到值准备就绪。如果值 x
对应于通道的发送,则布尔值 ok
为 true
,如果是一个零值,表示通道已关闭,则为 false
。
(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.
Send
方法在通道 v
上发送 x
。如果 v
的 Kind
不是 Chan
或者 x
的类型与 v
的元素类型不同,则会 panic。与 Go 语言类似,x
的值必须可分配给通道的元素类型。
(Value) Seq <- go1.23.0
|
|
Seq returns an iter.Seq[Value] that loops over the elements of v. If v’s kind is Func, it must be a function that has no results and that takes a single argument of type func(T) bool for some type T. If v’s kind is Pointer, the pointer element type must have kind Array. Otherwise v’s kind must be Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Array, Chan, Map, Slice, or String.
Seq 返回一个 iter.Seq[Value],遍历 v 的元素。如果 v 的 Kind 是 Func,那么该函数必须没有结果,并且接受一个类型为 func(T) bool
的参数,其中 T 是某种类型。如果 v 的 Kind 是 Pointer,则指针的元素类型必须是 Array。否则,v 的 Kind 必须是 Int、Int8、Int16、Int32、Int64、Uint、Uint8、Uint16、Uint32、Uint64、Uintptr、Array、Chan、Map、Slice 或 String。
(Value) Seq2 <- go1.23.0
|
|
Seq2 returns an iter.Seq2[Value, Value] that loops over the elements of v. If v’s kind is Func, it must be a function that has no results and that takes a single argument of type func(K, V) bool for some type K, V. If v’s kind is Pointer, the pointer element type must have kind Array. Otherwise v’s kind must be Array, Map, Slice, or String.
Seq2 返回一个 iter.Seq2[Value, Value],遍历 v 的元素。如果 v 的 Kind 是 Func,那么该函数必须没有结果,并且接受一个类型为 func(K, V) bool
的参数,其中 K 和 V 是某种类型。如果 v 的 Kind 是 Pointer,则指针的元素类型必须是 Array。否则,v 的 Kind 必须是 Array、Map、Slice 或 String。
(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 and must not be derived from an unexported field.
Set
方法将 x
赋值给值 v
。如果 CanSet
方法返回 false
,则会 panic。与 Go 语言类似,x
的值必须可分配给 v
的类型,且不能是未导出字段派生的。
(Value) SetBool
|
|
SetBool sets v’s underlying value. It panics if v’s Kind is not Bool or if CanSet() is false.
SetBool
方法设置 v
的底层值为 x
。如果 v
的 Kind
不是 Bool
或者 CanSet()
返回 false
,则会 panic。
(Value) SetBytes
|
|
SetBytes sets v’s underlying value. It panics if v’s underlying value is not a slice of bytes.
SetBytes
方法设置 v
的底层值为 x
。如果 v
的底层值不是字节切片,则会 panic。
(Value) SetCap <- go1.2
|
|
SetCap sets v’s capacity to n. It panics if v’s Kind is not Slice or if n is smaller than the length or greater than the capacity of the slice.
SetCap
方法将v
的容量设置为n
。如果v
的Kind
不是Slice
,或者n
小于切片的长度或大于切片的容量,它会引发panic。
(Value) SetComplex
|
|
SetComplex sets v’s underlying value to x. It panics if v’s Kind is not Complex64 or Complex128, or if CanSet() is false.
SetComplex
方法将 v
的底层值设置为 x
。如果 v
的 Kind
不是 Complex64
或 Complex128
,或者 CanSet()
返回 false
,则会 panic。
(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.
SetFloat
方法将 v
的底层值设置为 x
。如果 v
的 Kind
不是 Float32
或 Float64
,或者 CanSet()
返回 false
,则会 panic。
(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.
SetInt
方法将 v
的底层值设置为 x
。如果 v
的 Kind
不是 Int
、Int8
、Int16
、Int32
或 Int64
,或者 CanSet()
返回 false
,则会 panic。
(Value) SetIterKey <- go1.18
|
|
SetIterKey assigns to v the key of iter’s current map entry. It is equivalent to v.Set(iter.Key()), but it avoids allocating a new Value. As in Go, the key must be assignable to v’s type and must not be derived from an unexported field.
SetIterKey
方法将 iter
当前映射项的键赋值给 v
。它等价于 v.Set(iter.Key())
,但是它避免了分配新值。与 Go 语言类似,键必须可分配给 v
的类型,且不能是未导出字段派生的。
(Value) SetIterValue <- go1.18
|
|
SetIterValue assigns to v the value of iter’s current map entry. It is equivalent to v.Set(iter.Value()), but it avoids allocating a new Value. As in Go, the value must be assignable to v’s type and must not be derived from an unexported field.
SetIterValue
方法为 v
赋值为 iter
当前的键值。它等同于 v.Set(iter.Value())
,但是它避免了分配新的 Value。与 Go 一样,值必须可分配给 v 的类型,且不能从未公开的字段派生。
(Value) SetLen
|
|
SetLen sets v’s length to n. It panics if v’s Kind is not Slice or if n is negative or greater than the capacity of the slice.
SetLen
方法将 v
的长度设置为 n
。如果 v
的 Kind
不是 Slice
,或者 n
是负数或大于切片的容量,则会 panic。
(Value) SetMapIndex
|
|
SetMapIndex sets the element associated with key in the map v to elem. It panics if v’s Kind is not Map. If elem is the zero Value, SetMapIndex deletes the key from the map. Otherwise if v holds a nil map, SetMapIndex will panic. As in Go, key’s elem must be assignable to the map’s key type, and elem’s value must be assignable to the map’s elem type.
SetMapIndex
方法将与键 key
相关联的元素设置为 elem
。如果 v
的 Kind
不是 Map
,则会 panic。如果 elem
是零值,则 SetMapIndex
会从 map 中删除该key
。否则,如果 v
持有一个 nil
map,则 SetMapIndex
会 panic。与 Go 一样,key
的值必须可分配给 map 的键类型,elem
的值必须可分配给 map 的值类型。
(Value) SetPointer
|
|
SetPointer sets the unsafe.Pointer value v to x. It panics if v’s Kind is not UnsafePointer.
SetPointer
方法将 unsafe.Pointer 值 x
分配给 v
。如果 v
的 Kind
不是 UnsafePointer
,则会 panic。
(Value) SetString
|
|
SetString sets v’s underlying value to x. It panics if v’s Kind is not String or if CanSet() is false.
SetString
方法将 v
的底层值设置为 x
。如果 v
的 Kind
不是 String
或 CanSet()
为 false
,则会 panic。
(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.
SetUint
方法将 v
的底层值设置为 x
。如果 v
的 Kind
不是 Uint
,Uintptr
,Uint8
,Uint16
,Uint32
或 Uint64
或 CanSet()
为 false
,则会 panic。
(Value) SetZero <- go1.20
|
|
SetZero sets v to be the zero value of v’s type. It panics if CanSet returns false.
SetZero
方法将 v
设置为 v
类型的零值。如果 CanSet
返回 false
,则会 panic。
(Value) Slice
|
|
Slice returns v[i:j]. It panics if v’s Kind is not Array, Slice or String, or if v is an unaddressable array, or if the indexes are out of bounds.
Slice
方法返回 v[i:j]
。如果 v
的 Kind
不是 Array
,Slice
或 String
,或者 v
是不可寻址的数组,或者索引超出范围,则会 panic。
(Value) Slice3 <- go1.2
|
|
Slice3 is the 3-index form of the slice operation: it returns v[i:j:k]. It panics if v’s Kind is not Array or Slice, or if v is an unaddressable array, or if the indexes are out of bounds.
Slice3
方法是 slice
操作的三个索引形式:它返回 v[i:j:k]
。如果 v
的 Kind
不是 Array
或 Slice
,或者 v
是不可寻址的数组,或者索引超出范围,则会 panic。
(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 “
String
方法将v
的底层值作为string
返回。String
方法是一个特殊情况,因为Go的String
方法约定。与其他的getter不同,如果v
的Kind
不是String
,它不会抛出panic。相反,它返回一个字符串"<T value>
",其中T
是v
的类型。fmt
包对Values
进行了特殊处理。它(指的是fmt
包)不会隐式调用它们的String
方法,而是打印它们所持有的具体值。
(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 delivers a value, x is the transferred value and ok is true. If the receive cannot finish without blocking, x is the zero Value and ok is false. If the channel is closed, x is the zero value for the channel’s element type and ok is false.
TryRecv
方法尝试从通道v
接收一个值,但不会阻塞。如果接收到值,则x
是传输的值,ok
为true
。如果接收无法完成而不阻塞,则x
是零值,并且ok
为false。如果通道关闭,则x
是通道元素类型的零值,ok
为false
。如果v
的Kind
不是Chan
,它将panic。
(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 reports whether the value was sent. As in Go, x’s value must be assignable to the channel’s element type.
TrySend
方法尝试在通道v
上发送x
,但不会阻塞。如果v
的Kind
不是Chan
,它将panic。它报告值是否已发送。与Go中一样,x
的值必须可分配给通道的元素类型。
(Value) Type
|
|
Type returns v’s type.
Type
方法返回v
的类型。
(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.
Uint
方法将v
的底层值作为uint64
返回。如果v
的Kind不是Uint
、Uintptr
、Uint8
、Uint16
、Uint32
或Uint64
,它将引发panic。
(Value) UnsafeAddr
|
|
UnsafeAddr returns a pointer to v’s data, as a uintptr. It panics if v is not addressable.
UnsafeAddr
方法返回指向v
的数据的指针,作为uintptr
。如果v
不可寻址,则它将引发panic。
It’s preferred to use uintptr(Value.Addr().UnsafePointer()) to get the equivalent result.
最好使用uintptr(Value.Addr().UnsafePointer())
来获得等效的结果。
(Value) UnsafePointer <- go1.18
|
|
UnsafePointer returns v’s value as a unsafe.Pointer. It panics if v’s Kind is not Chan, Func, Map, Pointer, Slice, or UnsafePointer.
UnsafePointer
方法将v
的值作为unsafe.Pointer返回。如果v
的Kind
不是Chan
、Func
、Map
、Pointer
、Slice
或UnsafePointer
,它将panic。
If v’s Kind is Func, the returned pointer is an underlying code pointer, but not necessarily enough to identify a single function uniquely. The only guarantee is that the result is zero if and only if v is a nil func Value.
如果v
的Kind
是Func
,则返回的指针是底层的代码指针,但不一定足以唯一地标识单个函数。唯一的保证是,当且仅当v
是一个nil
函数Value
时,结果为零。
If v’s Kind is Slice, the returned pointer is to the first element of the slice. If the slice is nil the returned value is nil. If the slice is empty but non-nil the return value is non-nil.
如果v
的Kind
是Slice
,则返回的指针指向切片的第一个元素。如果切片是nil
,则返回值为nil
。如果切片为空但非nil
,则返回值为非nil
。
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.
ValueError
结构体在对不支持它的Value
调用Value
方法时发生。这些情况在每个方法的描述中都有记录。
(*ValueError) Error
|
|
Notes
Bugs
- FieldByName and related functions consider struct field names to be equal if the names are equal, even if they are unexported names originating in different packages. The practical effect of this is that the result of t.FieldByName(“x”) is not well defined if the struct type t contains multiple fields named x (embedded from different packages). FieldByName may return one of the fields named x or may report that there are none. See https://golang.org/issue/4876 for more details.
FieldByName
和相关函数认为结构体字段名称相等,即使它们是来自不同包的未导出名称。这样的实际影响是,如果结构体类型t
包含多个名为x
的字段(来自不同的包),则t.FieldByName("x")
的结果不是定义良好的。FieldByName
可能返回一个名为x
的字段,也可能报告没有任何字段。有关更多详细信息,请参见https://golang.org/issue/4876。