github.com/3JoB/go-reflect@v1.0.1/reflect.go (about) 1 package reflect 2 3 import ( 4 "reflect" 5 "unsafe" 6 ) 7 8 // Type is the representation of a Go type. 9 // 10 // Not all methods apply to all kinds of types. Restrictions, 11 // if any, are noted in the documentation for each method. 12 // Use the Kind method to find out the kind of type before 13 // calling kind-specific methods. Calling a method 14 // inappropriate to the kind of type causes a run-time panic. 15 // 16 // Type values are comparable, such as with the == operator, 17 // so they can be used as map keys. 18 // Two Type values are equal if they represent identical types. 19 type Type = *rtype 20 21 type rtype struct{} 22 23 type flag uintptr 24 25 const ( 26 flagKindWidth = 5 // there are 27 kinds 27 flagKindMask flag = 1<<flagKindWidth - 1 28 flagStickyRO flag = 1 << 5 29 flagEmbedRO flag = 1 << 6 30 flagIndir flag = 1 << 7 31 flagAddr flag = 1 << 8 32 flagMethod flag = 1 << 9 33 flagMethodShift = 10 34 flagRO flag = flagStickyRO | flagEmbedRO 35 ) 36 37 // A Kind represents the specific kind of type that a Type represents. 38 // The zero Kind is not a valid kind. 39 type Kind = reflect.Kind 40 41 const ( 42 Invalid Kind = iota 43 Bool 44 Int 45 Int8 46 Int16 47 Int32 48 Int64 49 Uint 50 Uint8 51 Uint16 52 Uint32 53 Uint64 54 Uintptr 55 Float32 56 Float64 57 Complex64 58 Complex128 59 Array 60 Chan 61 Func 62 Interface 63 Map 64 Ptr 65 Slice 66 String 67 Struct 68 UnsafePointer 69 ) 70 71 const ( 72 _ SelectDir = iota 73 SelectSend // case Chan <- Send 74 SelectRecv // case <-Chan: 75 SelectDefault // default 76 ) 77 78 // A StructTag is the tag string in a struct field. 79 // 80 // By convention, tag strings are a concatenation of 81 // optionally space-separated key:"value" pairs. 82 // Each key is a non-empty string consisting of non-control 83 // characters other than space (U+0020 ' '), quote (U+0022 '"'), 84 // and colon (U+003A ':'). Each value is quoted using U+0022 '"' 85 // characters and Go string literal syntax. 86 type StructTag = reflect.StructTag 87 88 // ChanDir represents a channel type's direction. 89 type ChanDir = reflect.ChanDir 90 91 const ( 92 RecvDir ChanDir = 1 << iota // <-chan 93 SendDir // chan<- 94 BothDir = RecvDir | SendDir // chan 95 ) 96 97 // A MapIter is an iterator for ranging over a map. 98 // See Value.MapRange. 99 type MapIter = reflect.MapIter 100 101 // A ValueError occurs when a Value method is invoked on 102 // a Value that does not support it. Such cases are documented 103 // in the description of each method. 104 type ValueError = reflect.ValueError 105 106 // SliceHeader is the runtime representation of a slice. 107 // It cannot be used safely or portably and its representation may 108 // change in a later release. 109 // Moreover, the Data field is not sufficient to guarantee the data 110 // it references will not be garbage collected, so programs must keep 111 // a separate, correctly typed pointer to the underlying data. 112 type SliceHeader = reflect.SliceHeader 113 114 // StringHeader is the runtime representation of a string. 115 // It cannot be used safely or portably and its representation may 116 // change in a later release. 117 // Moreover, the Data field is not sufficient to guarantee the data 118 // it references will not be garbage collected, so programs must keep 119 // a separate, correctly typed pointer to the underlying data. 120 type StringHeader = reflect.StringHeader 121 122 // A SelectCase describes a single case in a select operation. 123 // The kind of case depends on Dir, the communication direction. 124 // 125 // If Dir is SelectDefault, the case represents a default case. 126 // Chan and Send must be zero Values. 127 // 128 // If Dir is SelectSend, the case represents a send operation. 129 // Normally Chan's underlying value must be a channel, and Send's underlying value must be 130 // assignable to the channel's element type. As a special case, if Chan is a zero Value, 131 // then the case is ignored, and the field Send will also be ignored and may be either zero 132 // or non-zero. 133 // 134 // If Dir is SelectRecv, the case represents a receive operation. 135 // Normally Chan's underlying value must be a channel and Send must be a zero Value. 136 // If Chan is a zero Value, then the case is ignored, but Send must still be a zero Value. 137 // When a receive operation is selected, the received Value is returned by Select. 138 type SelectCase struct { 139 Dir SelectDir // direction of case 140 Chan Value // channel to use (for send or receive) 141 Send Value // value to send (for send) 142 } 143 144 type SelectDir = reflect.SelectDir 145 146 // Value is the reflection interface to a Go value. 147 // Not all methods apply to all kinds of values. 148 // Restrictions, if any, are noted in the documentation for each method. 149 // Use the Kind method to find out the kind of value before calling kind-specific methods. 150 // Calling a method inappropriate to the kind of type causes a run time panic. 151 // The zero Value represents no value. 152 // Its IsValid method returns false, its Kind method returns Invalid, 153 // its String method returns "<invalid Value>", and all other methods panic. 154 // Most functions and methods never return an invalid value. 155 // If one does, its documentation states the conditions explicitly. 156 // A Value can be used concurrently by multiple goroutines provided that 157 // the underlying Go value can be used concurrently for the equivalent direct operations. 158 // To compare two Values, compare the results of the Interface method. 159 // Using == on two Values does not compare the underlying values they represent. 160 type Value struct { 161 typ Type 162 ptr unsafe.Pointer 163 flag 164 } 165 166 // Method represents a single method. 167 type Method struct { 168 // Name is the method name. 169 // PkgPath is the package path that qualifies a lower case (unexported) 170 // method name. It is empty for upper case (exported) method names. 171 // The combination of PkgPath and Name uniquely identifies a method 172 // in a method set. 173 // See https://golang.org/ref/spec#Uniqueness_of_identifiers 174 Name string 175 176 PkgPath string 177 178 Type Type // method type 179 Func Value // func with receiver as first argument 180 Index int // index for Type.Method 181 } 182 183 // A StructField describes a single field in a struct. 184 type StructField struct { 185 // Name is the field name. 186 Name string 187 188 // PkgPath is the package path that qualifies a lower case (unexported) 189 // field name. It is empty for upper case (exported) field names. 190 // See https://golang.org/ref/spec#Uniqueness_of_identifiers 191 PkgPath string 192 193 Type Type // field type 194 Tag StructTag // field tag string 195 Offset uintptr // offset within struct, in bytes 196 Index []int // index sequence for Type.FieldByIndex 197 Anonymous bool // is an embedded field 198 } 199 200 // IsExported reports whether the field is exported. 201 func (f StructField) IsExported() bool { 202 return f.PkgPath == "" 203 } 204 205 // ArrayOf returns the array type with the given count and element type. 206 // For example, if t represents int, ArrayOf(5, t) represents [5]int. 207 // 208 // If the resulting type would be larger than the available address space, 209 // ArrayOf panics. 210 func ArrayOf(count int, elem Type) Type { 211 return arrayOf(count, elem) 212 } 213 214 // ChanOf returns the channel type with the given direction and element type. 215 // For example, if t represents int, ChanOf(RecvDir, t) represents <-chan int. 216 // 217 // The gc runtime imposes a limit of 64 kB on channel element types. 218 // If t's size is equal to or exceeds this limit, ChanOf panics. 219 func ChanOf(dir ChanDir, t Type) Type { 220 return chanOf(dir, t) 221 } 222 223 // FuncOf returns the function type with the given argument and result types. 224 // For example if k represents int and e represents string, 225 // FuncOf([]Type{k}, []Type{e}, false) represents func(int) string. 226 // 227 // The variadic argument controls whether the function is variadic. FuncOf 228 // panics if the in[len(in)-1] does not represent a slice and variadic is 229 // true. 230 func FuncOf(in, out []Type, variadic bool) Type { 231 return funcOf(in, out, variadic) 232 } 233 234 // MapOf returns the map type with the given key and element types. 235 // For example, if k represents int and e represents string, 236 // MapOf(k, e) represents map[int]string. 237 // 238 // If the key type is not a valid map key type (that is, if it does 239 // not implement Go's == operator), MapOf panics. 240 func MapOf(key, elem Type) Type { 241 return mapOf(key, elem) 242 } 243 244 // PtrTo returns the pointer type with element t. 245 // For example, if t represents type Foo, PtrTo(t) represents *Foo. 246 func PtrTo(t Type) Type { 247 return ptrTo(t) 248 } 249 250 // SliceOf returns the slice type with element type t. 251 // For example, if t represents int, SliceOf(t) represents []int. 252 func SliceOf(t Type) Type { 253 return sliceOf(t) 254 } 255 256 // StructOf returns the struct type containing fields. 257 // The Offset and Index fields are ignored and computed as they would be 258 // by the compiler. 259 // 260 // StructOf currently does not generate wrapper methods for embedded 261 // fields and panics if passed unexported StructFields. 262 // These limitations may be lifted in a future version. 263 func StructOf(fields []StructField) Type { 264 return structOf(fields) 265 } 266 267 // TypeOf returns the reflection Type that represents the dynamic type of i. 268 // If i is a nil interface value, TypeOf returns nil. 269 func TypeOf(v any) Type { 270 value := (*Value)(unsafe.Pointer(&v)) 271 return value.typ 272 } 273 274 // TypeID returns unique type identifier of v. 275 func TypeID(v any) uintptr { 276 return uintptr(unsafe.Pointer(TypeOf(v))) 277 } 278 279 func valueOf(v any) Value { 280 if v == nil { 281 return Value{} 282 } 283 valueLayout := (*Value)(unsafe.Pointer(&v)) 284 value := Value{} 285 value.typ = valueLayout.typ 286 value.ptr = valueLayout.ptr 287 f := flag(value.typ.Kind()) 288 if ifaceIndir(value.typ) { 289 f |= flagIndir 290 } 291 value.flag = f 292 return value 293 } 294 295 // TypeAndPtrOf returns raw Type and ptr value in favor of performance. 296 func TypeAndPtrOf(v any) (Type, unsafe.Pointer) { 297 value := (*Value)(unsafe.Pointer(&v)) 298 return value.typ, value.ptr 299 } 300 301 // ValueOf returns a new Value initialized to the concrete value 302 // stored in the interface i. ValueOf(nil) returns the zero Value. 303 func ValueOf(v any) Value { 304 escape(v) 305 return valueOf(v) 306 } 307 308 // ValueNoEscapeOf no escape of ValueOf. 309 func ValueNoEscapeOf(v any) Value { 310 return valueOf(v) 311 } 312 313 // ToReflectType convert Type to reflect.Type 314 func ToReflectType(t Type) reflect.Type { 315 return toRT(t) 316 } 317 318 // ToReflectValue convert Value to reflect.Value 319 func ToReflectValue(v Value) reflect.Value { 320 return toRV(v) 321 } 322 323 // ToType convert reflect.Type to Type 324 func ToType(t reflect.Type) Type { 325 return toT(t) 326 } 327 328 // ToValue convert reflect.Value to Value 329 func ToValue(v reflect.Value) Value { 330 return toV(v) 331 } 332 333 // Copy copies the contents of src into dst until either 334 // dst has been filled or src has been exhausted. 335 // It returns the number of elements copied. 336 // Dst and src each must have kind Slice or Array, and 337 // dst and src must have the same element type. 338 // 339 // As a special case, src can have kind String if the element type of dst is kind Uint8. 340 func Copy(dst, src Value) int { 341 return value_Copy(dst, src) 342 } 343 344 // DeepEqual reports whether x and y are “deeply equal,” defined as follows. 345 // Two values of identical type are deeply equal if one of the following cases applies. 346 // Values of distinct types are never deeply equal. 347 // 348 // Array values are deeply equal when their corresponding elements are deeply equal. 349 // 350 // Struct values are deeply equal if their corresponding fields, 351 // both exported and unexported, are deeply equal. 352 // 353 // Func values are deeply equal if both are nil; otherwise they are not deeply equal. 354 // 355 // Interface values are deeply equal if they hold deeply equal concrete values. 356 // 357 // Map values are deeply equal when all of the following are true: 358 // they are both nil or both non-nil, they have the same length, 359 // and either they are the same map object or their corresponding keys 360 // (matched using Go equality) map to deeply equal values. 361 // 362 // Pointer values are deeply equal if they are equal using Go's == operator 363 // or if they point to deeply equal values. 364 // 365 // Slice values are deeply equal when all of the following are true: 366 // they are both nil or both non-nil, they have the same length, 367 // and either they point to the same initial entry of the same underlying array 368 // (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. 369 // Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) 370 // are not deeply equal. 371 // 372 // Other values - numbers, bools, strings, and channels - are deeply equal 373 // if they are equal using Go's == operator. 374 // 375 // In general DeepEqual is a recursive relaxation of Go's == operator. 376 // However, this idea is impossible to implement without some inconsistency. 377 // Specifically, it is possible for a value to be unequal to itself, 378 // either because it is of func type (uncomparable in general) 379 // or because it is a floating-point NaN value (not equal to itself in floating-point comparison), 380 // or because it is an array, struct, or interface containing 381 // such a value. 382 // On the other hand, pointer values are always equal to themselves, 383 // even if they point at or contain such problematic values, 384 // because they compare equal using Go's == operator, and that 385 // is a sufficient condition to be deeply equal, regardless of content. 386 // DeepEqual has been defined so that the same short-cut applies 387 // to slices and maps: if x and y are the same slice or the same map, 388 // they are deeply equal regardless of content. 389 // 390 // As DeepEqual traverses the data values it may find a cycle. The 391 // second and subsequent times that DeepEqual compares two pointer 392 // values that have been compared before, it treats the values as 393 // equal rather than examining the values to which they point. 394 // This ensures that DeepEqual terminates. 395 func DeepEqual(x, y any) bool { 396 return reflect.DeepEqual(x, y) 397 } 398 399 func Swapper(slice any) func(i, j int) { 400 return reflect.Swapper(slice) 401 } 402 403 // Append appends the values x to a slice s and returns the resulting slice. 404 // As in Go, each x's value must be assignable to the slice's element type. 405 func Append(s Value, x ...Value) Value { 406 return value_Append(s, x...) 407 } 408 409 // AppendSlice appends a slice t to a slice s and returns the resulting slice. 410 // The slices s and t must have the same element type. 411 func AppendSlice(s, t Value) Value { 412 return value_AppendSlice(s, t) 413 } 414 415 // Indirect returns the value that v points to. 416 // If v is a nil pointer, Indirect returns a zero Value. 417 // If v is not a pointer, Indirect returns v. 418 func Indirect(v Value) Value { 419 return value_Indirect(v) 420 } 421 422 // MakeChan creates a new channel with the specified type and buffer size. 423 func MakeChan(typ Type, buffer int) Value { 424 return value_MakeChan(typ, buffer) 425 } 426 427 // MakeFunc returns a new function of the given Type 428 // that wraps the function fn. When called, that new function 429 // does the following: 430 // 431 // - converts its arguments to a slice of Values. 432 // - runs results := fn(args). 433 // - returns the results as a slice of Values, one per formal result. 434 // 435 // The implementation fn can assume that the argument Value slice 436 // has the number and type of arguments given by typ. 437 // If typ describes a variadic function, the final Value is itself 438 // a slice representing the variadic arguments, as in the 439 // body of a variadic function. The result Value slice returned by fn 440 // must have the number and type of results given by typ. 441 // 442 // The Value.Call method allows the caller to invoke a typed function 443 // in terms of Values; in contrast, MakeFunc allows the caller to implement 444 // a typed function in terms of Values. 445 // 446 // The Examples section of the documentation includes an illustration 447 // of how to use MakeFunc to build a swap function for different types. 448 func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value { 449 return value_MakeFunc(typ, fn) 450 } 451 452 // MakeMap creates a new map with the specified type. 453 func MakeMap(typ Type) Value { 454 return value_MakeMap(typ) 455 } 456 457 // MakeMapWithSize creates a new map with the specified type 458 // and initial space for approximately n elements. 459 func MakeMapWithSize(typ Type, n int) Value { 460 return value_MakeMapWithSize(typ, n) 461 } 462 463 // MakeSlice creates a new zero-initialized slice value 464 // for the specified slice type, length, and capacity. 465 func MakeSlice(typ Type, len, cap int) Value { 466 return value_MakeSlice(typ, len, cap) 467 } 468 469 // New returns a Value representing a pointer to a new zero value 470 // for the specified type. That is, the returned Value's Type is PtrTo(typ). 471 func New(typ Type) Value { 472 return value_New(typ) 473 } 474 475 // NewAt returns a Value representing a pointer to a value of the 476 // specified type, using p as that pointer. 477 func NewAt(typ Type, p unsafe.Pointer) Value { 478 return value_NewAt(typ, p) 479 } 480 481 // Select executes a select operation described by the list of cases. 482 // Like the Go select statement, it blocks until at least one of the cases 483 // can proceed, makes a uniform pseudo-random choice, 484 // and then executes that case. It returns the index of the chosen case 485 // and, if that case was a receive operation, the value received and a 486 // boolean indicating whether the value corresponds to a send on the channel 487 // (as opposed to a zero value received because the channel is closed). 488 func Select(cases []SelectCase) (int, Value, bool) { 489 return value_Select(cases) 490 } 491 492 // Zero returns a Value representing the zero value for the specified type. 493 // The result is different from the zero value of the Value struct, 494 // which represents no value at all. 495 // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0. 496 // The returned value is neither addressable nor settable. 497 func Zero(typ Type) Value { 498 return value_Zero(typ) 499 } 500 501 // Align returns the alignment in bytes of a value of 502 // this type when allocated in memory. 503 func (t *rtype) Align() int { 504 return type_Align(t) 505 } 506 507 // FieldAlign returns the alignment in bytes of a value of 508 // this type when used as a field in a struct. 509 func (t *rtype) FieldAlign() int { 510 return type_FieldAlign(t) 511 } 512 513 // Method returns the i'th method in the type's method set. 514 // It panics if i is not in the range [0, NumMethod()). 515 // 516 // For a non-interface type T or *T, the returned Method's Type and Func 517 // fields describe a function whose first argument is the receiver. 518 // 519 // For an interface type, the returned Method's Type field gives the 520 // method signature, without a receiver, and the Func field is nil. 521 // 522 // Only exported methods are accessible and they are sorted in 523 // lexicographic order. 524 func (t *rtype) Method(a0 int) Method { 525 return toM(type_Method(t, a0)) 526 } 527 528 // MethodByName returns the method with that name in the type's 529 // method set and a boolean indicating if the method was found. 530 // 531 // For a non-interface type T or *T, the returned Method's Type and Func 532 // fields describe a function whose first argument is the receiver. 533 // 534 // For an interface type, the returned Method's Type field gives the 535 // method signature, without a receiver, and the Func field is nil. 536 func (t *rtype) MethodByName(a0 string) (Method, bool) { 537 mtd, ok := type_MethodByName(t, a0) 538 return toM(mtd), ok 539 } 540 541 // NumMethod returns the number of exported methods in the type's method set. 542 func (t *rtype) NumMethod() int { 543 return type_NumMethod(t) 544 } 545 546 // Name returns the type's name within its package for a defined type. 547 // For other (non-defined) types it returns the empty string. 548 func (t *rtype) Name() string { 549 return type_Name(t) 550 } 551 552 // PkgPath returns a defined type's package path, that is, the import path 553 // that uniquely identifies the package, such as "encoding/base64". 554 // If the type was predeclared (string, error) or not defined (*T, struct{}, 555 // []int, or A where A is an alias for a non-defined type), the package path 556 // will be the empty string. 557 func (t *rtype) PkgPath() string { 558 return type_PkgPath(t) 559 } 560 561 // Size returns the number of bytes needed to store 562 // a value of the given type; it is analogous to unsafe.Sizeof. 563 func (t *rtype) Size() uintptr { 564 return type_Size(t) 565 } 566 567 // String returns a string representation of the type. 568 // The string representation may use shortened package names 569 // (e.g., base64 instead of "encoding/base64") and is not 570 // guaranteed to be unique among types. To test for type identity, 571 // compare the Types directly. 572 func (t *rtype) String() string { 573 return type_String(t) 574 } 575 576 // Kind returns the specific kind of this type. 577 func (t *rtype) Kind() Kind { 578 return type_Kind(t) 579 } 580 581 // Implements reports whether the type implements the interface type u. 582 func (t *rtype) Implements(u Type) bool { 583 return type_Implements(t, toRT(u)) 584 } 585 586 // AssignableTo reports whether a value of the type is assignable to type u. 587 func (t *rtype) AssignableTo(u Type) bool { 588 return type_AssignableTo(t, toRT(u)) 589 } 590 591 // ConvertibleTo reports whether a value of the type is convertible to type u. 592 func (t *rtype) ConvertibleTo(u Type) bool { 593 return type_ConvertibleTo(t, toRT(u)) 594 } 595 596 // Comparable reports whether values of this type are comparable. 597 func (t *rtype) Comparable() bool { 598 return type_Comparable(t) 599 } 600 601 // Methods applicable only to some types, depending on Kind. 602 // The methods allowed for each kind are: 603 // 604 // Int*, Uint*, Float*, Complex*: Bits 605 // Array: Elem, Len 606 // Chan: ChanDir, Elem 607 // Func: In, NumIn, Out, NumOut, IsVariadic. 608 // Map: Key, Elem 609 // Ptr: Elem 610 // Slice: Elem 611 // Struct: Field, FieldByIndex, FieldByName, FieldByNameFunc, NumField 612 613 // Bits returns the size of the type in bits. 614 // It panics if the type's Kind is not one of the 615 // sized or unsized Int, Uint, Float, or Complex kinds. 616 func (t *rtype) Bits() int { 617 return type_Bits(t) 618 } 619 620 // ChanDir returns a channel type's direction. 621 // It panics if the type's Kind is not Chan. 622 func (t *rtype) ChanDir() ChanDir { 623 return type_ChanDir(t) 624 } 625 626 // IsVariadic reports whether a function type's final input parameter 627 // is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's 628 // implicit actual type []T. 629 // 630 // For concreteness, if t represents func(x int, y ... float64), then 631 // 632 // t.NumIn() == 2 633 // t.In(0) is the reflect.Type for "int" 634 // t.In(1) is the reflect.Type for "[]float64" 635 // t.IsVariadic() == true 636 // 637 // IsVariadic panics if the type's Kind is not Func. 638 func (t *rtype) IsVariadic() bool { 639 return type_IsVariadic(t) 640 } 641 642 // Elem returns a type's element type. 643 // It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice. 644 func (t *rtype) Elem() Type { 645 return ToType(type_Elem(t)) 646 } 647 648 // Field returns a struct type's i'th field. 649 // It panics if the type's Kind is not Struct. 650 // It panics if i is not in the range [0, NumField()). 651 func (t *rtype) Field(i int) StructField { 652 return toSF(type_Field(t, i)) 653 } 654 655 // FieldByIndex returns the nested field corresponding 656 // to the index sequence. It is equivalent to calling Field 657 // successively for each index i. 658 // It panics if the type's Kind is not Struct. 659 func (t *rtype) FieldByIndex(index []int) StructField { 660 return toSF(type_FieldByIndex(t, index)) 661 } 662 663 // FieldByName returns the struct field with the given name 664 // and a boolean indicating if the field was found. 665 func (t *rtype) FieldByName(name string) (StructField, bool) { 666 field, ok := type_FieldByName(t, name) 667 return toSF(field), ok 668 } 669 670 // FieldByNameFunc returns the struct field with a name 671 // that satisfies the match function and a boolean indicating if 672 // the field was found. 673 // 674 // FieldByNameFunc considers the fields in the struct itself 675 // and then the fields in any embedded structs, in breadth first order, 676 // stopping at the shallowest nesting depth containing one or more 677 // fields satisfying the match function. If multiple fields at that depth 678 // satisfy the match function, they cancel each other 679 // and FieldByNameFunc returns no match. 680 // This behavior mirrors Go's handling of name lookup in 681 // structs containing embedded fields. 682 func (t *rtype) FieldByNameFunc(match func(string) bool) (StructField, bool) { 683 field, ok := type_FieldByNameFunc(t, match) 684 return toSF(field), ok 685 } 686 687 // In returns the type of a function type's i'th input parameter. 688 // It panics if the type's Kind is not Func. 689 // It panics if i is not in the range [0, NumIn()). 690 func (t *rtype) In(i int) Type { 691 return ToType(type_In(t, i)) 692 } 693 694 // Key returns a map type's key type. 695 // It panics if the type's Kind is not Map. 696 func (t *rtype) Key() Type { 697 return ToType(type_Key(t)) 698 } 699 700 // Len returns an array type's length. 701 // It panics if the type's Kind is not Array. 702 func (t *rtype) Len() int { 703 return type_Len(t) 704 } 705 706 // NumField returns a struct type's field count. 707 // It panics if the type's Kind is not Struct. 708 func (t *rtype) NumField() int { 709 return type_NumField(t) 710 } 711 712 // NumIn returns a function type's input parameter count. 713 // It panics if the type's Kind is not Func. 714 func (t *rtype) NumIn() int { 715 return type_NumIn(t) 716 } 717 718 // NumOut returns a function type's output parameter count. 719 // It panics if the type's Kind is not Func. 720 func (t *rtype) NumOut() int { 721 return type_NumOut(t) 722 } 723 724 // Out returns the type of a function type's i'th output parameter. 725 // It panics if the type's Kind is not Func. 726 // It panics if i is not in the range [0, NumOut()). 727 func (t *rtype) Out(i int) Type { 728 return toT(type_Out(t, i)) 729 } 730 731 // Addr returns a pointer value representing the address of v. 732 // It panics if CanAddr() returns false. 733 // Addr is typically used to obtain a pointer to a struct field 734 // or slice element in order to call a method that requires a 735 // pointer receiver. 736 func (v Value) Addr() Value { 737 return value_Addr(v) 738 } 739 740 // Bool returns v's underlying value. 741 // It panics if v's kind is not Bool. 742 func (v Value) Bool() bool { 743 return value_Bool(v) 744 } 745 746 // Bytes returns v's underlying value. 747 // It panics if v's underlying value is not a slice of bytes. 748 func (v Value) Bytes() []byte { 749 return value_Bytes(v) 750 } 751 752 // Call calls the function v with the input arguments in. 753 // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]). 754 // Call panics if v's Kind is not Func. 755 // It returns the output results as Values. 756 // As in Go, each input argument must be assignable to the 757 // type of the function's corresponding input parameter. 758 // If v is a variadic function, Call creates the variadic slice parameter 759 // itself, copying in the corresponding values. 760 func (v Value) Call(in []Value) []Value { 761 return value_Call(v, in) 762 } 763 764 // CallSlice calls the variadic function v with the input arguments in, 765 // assigning the slice in[len(in)-1] to v's final variadic argument. 766 // For example, if len(in) == 3, v.CallSlice(in) represents the Go call v(in[0], in[1], in[2]...). 767 // CallSlice panics if v's Kind is not Func or if v is not variadic. 768 // It returns the output results as Values. 769 // As in Go, each input argument must be assignable to the 770 // type of the function's corresponding input parameter. 771 func (v Value) CallSlice(in []Value) []Value { 772 return value_CallSlice(v, in) 773 } 774 775 // CanAddr reports whether the value's address can be obtained with Addr. 776 // Such values are called addressable. A value is addressable if it is 777 // an element of a slice, an element of an addressable array, 778 // a field of an addressable struct, or the result of dereferencing a pointer. 779 // If CanAddr returns false, calling Addr will panic. 780 func (v Value) CanAddr() bool { 781 return value_CanAddr(v) 782 } 783 784 // CanInterface reports whether Interface can be used without panicking. 785 func (v Value) CanInterface() bool { 786 return value_CanInterface(v) 787 } 788 789 // CanSet reports whether the value of v can be changed. 790 // A Value can be changed only if it is addressable and was not 791 // obtained by the use of unexported struct fields. 792 // If CanSet returns false, calling Set or any type-specific 793 // setter (e.g., SetBool, SetInt) will panic. 794 func (v Value) CanSet() bool { 795 return value_CanSet(v) 796 } 797 798 // Cap returns v's capacity. 799 // It panics if v's Kind is not Array, Chan, or Slice. 800 func (v Value) Cap() int { 801 return value_Cap(v) 802 } 803 804 // Close closes the channel v. 805 // It panics if v's Kind is not Chan. 806 func (v Value) Close() { 807 value_Close(v) 808 } 809 810 // Complex returns v's underlying value, as a complex128. 811 // It panics if v's Kind is not Complex64 or Complex128. 812 func (v Value) Complex() complex128 { 813 return value_Complex(v) 814 } 815 816 // Convert returns the value v converted to type t. 817 // If the usual Go conversion rules do not allow conversion 818 // of the value v to type t, Convert panics. 819 func (v Value) Convert(t Type) Value { 820 return value_Convert(v, t) 821 } 822 823 // Elem returns the value that the interface v contains 824 // or that the pointer v points to. 825 // It panics if v's Kind is not Interface or Ptr. 826 // It returns the zero Value if v is nil. 827 func (v Value) Elem() Value { 828 return value_Elem(v) 829 } 830 831 // Field returns the i'th field of the struct v. 832 // It panics if v's Kind is not Struct or i is out of range. 833 func (v Value) Field(i int) Value { 834 return value_Field(v, i) 835 } 836 837 // FieldByIndex returns the nested field corresponding to index. 838 // It panics if v's Kind is not struct. 839 func (v Value) FieldByIndex(index []int) Value { 840 return value_FieldByIndex(v, index) 841 } 842 843 // FieldByName returns the struct field with the given name. 844 // It returns the zero Value if no field was found. 845 // It panics if v's Kind is not struct. 846 func (v Value) FieldByName(name string) Value { 847 return value_FieldByName(v, name) 848 } 849 850 // FieldByNameFunc returns the struct field with a name 851 // that satisfies the match function. 852 // It panics if v's Kind is not struct. 853 // It returns the zero Value if no field was found. 854 func (v Value) FieldByNameFunc(match func(string) bool) Value { 855 return value_FieldByNameFunc(v, match) 856 } 857 858 // Float returns v's underlying value, as a float64. 859 // It panics if v's Kind is not Float32 or Float64. 860 func (v Value) Float() float64 { 861 return value_Float(v) 862 } 863 864 // Index returns v's i'th element. 865 // It panics if v's Kind is not Array, Slice, or String or i is out of range. 866 func (v Value) Index(i int) Value { 867 return value_Index(v, i) 868 } 869 870 // Int returns v's underlying value, as an int64. 871 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64. 872 func (v Value) Int() int64 { 873 return value_Int(v) 874 } 875 876 // Interface returns v's current value as an interface{}. 877 // It is equivalent to: 878 // 879 // var i interface{} = (v's underlying value) 880 // 881 // It panics if the Value was obtained by accessing 882 // unexported struct fields. 883 func (v Value) Interface() any { 884 return value_Interface(v) 885 } 886 887 // InterfaceData returns the interface v's value as a uintptr pair. 888 // It panics if v's Kind is not Interface. 889 func (v Value) InterfaceData() [2]uintptr { 890 return value_InterfaceData(v) 891 } 892 893 // IsNil reports whether its argument v is nil. The argument must be 894 // a chan, func, interface, map, pointer, or slice value; if it is 895 // not, IsNil panics. Note that IsNil is not always equivalent to a 896 // regular comparison with nil in Go. For example, if v was created 897 // by calling ValueOf with an uninitialized interface variable i, 898 // i==nil will be true but v.IsNil will panic as v will be the zero 899 // Value. 900 func (v Value) IsNil() bool { 901 return value_IsNil(v) 902 } 903 904 // IsValid reports whether v represents a value. 905 // It returns false if v is the zero Value. 906 // If IsValid returns false, all other methods except String panic. 907 // Most functions and methods never return an invalid Value. 908 // If one does, its documentation states the conditions explicitly. 909 func (v Value) IsValid() bool { 910 return value_IsValid(v) 911 } 912 913 // Kind returns v's Kind. 914 // If v is the zero Value (IsValid returns false), Kind returns Invalid. 915 func (v Value) Kind() Kind { 916 return value_Kind(v) 917 } 918 919 // Len returns v's length. 920 // It panics if v's Kind is not Array, Chan, Map, Slice, or String. 921 func (v Value) Len() int { 922 return value_Len(v) 923 } 924 925 // MapIndex returns the value associated with key in the map v. 926 // It panics if v's Kind is not Map. 927 // It returns the zero Value if key is not found in the map or if v represents a nil map. 928 // As in Go, the key's value must be assignable to the map's key type. 929 func (v Value) MapIndex(key Value) Value { 930 return value_MapIndex(v, key) 931 } 932 933 // MapKeys returns a slice containing all the keys present in the map, 934 // in unspecified order. 935 // It panics if v's Kind is not Map. 936 // It returns an empty slice if v represents a nil map. 937 func (v Value) MapKeys() []Value { 938 return value_MapKeys(v) 939 } 940 941 // MapRange returns a range iterator for a map. 942 // It panics if v's Kind is not Map. 943 // 944 // Call Next to advance the iterator, and Key/Value to access each entry. 945 // Next returns false when the iterator is exhausted. 946 // MapRange follows the same iteration semantics as a range statement. 947 // 948 // Example: 949 // 950 // iter := reflect.ValueOf(m).MapRange() 951 // for iter.Next() { 952 // k := iter.Key() 953 // v := iter.Value() 954 // ... 955 // } 956 func (v Value) MapRange() *MapIter { 957 return value_MapRange(v) 958 } 959 960 // Method returns a function value corresponding to v's i'th method. 961 // The arguments to a Call on the returned function should not include 962 // a receiver; the returned function will always use v as the receiver. 963 // Method panics if i is out of range or if v is a nil interface value. 964 func (v Value) Method(i int) Value { 965 return value_Method(v, i) 966 } 967 968 // MethodByName returns a function value corresponding to the method 969 // of v with the given name. 970 // The arguments to a Call on the returned function should not include 971 // a receiver; the returned function will always use v as the receiver. 972 // It returns the zero Value if no method was found. 973 func (v Value) MethodByName(name string) Value { 974 return value_MethodByName(v, name) 975 } 976 977 // NumField returns the number of fields in the struct v. 978 // It panics if v's Kind is not Struct. 979 func (v Value) NumField() int { 980 return value_NumField(v) 981 } 982 983 // NumMethod returns the number of exported methods in the value's method set. 984 func (v Value) NumMethod() int { 985 return value_NumMethod(v) 986 } 987 988 // OverflowComplex reports whether the complex128 x cannot be represented by v's type. 989 // It panics if v's Kind is not Complex64 or Complex128. 990 func (v Value) OverflowComplex(x complex128) bool { 991 return value_OverflowComplex(v, x) 992 } 993 994 // OverflowFloat reports whether the float64 x cannot be represented by v's type. 995 // It panics if v's Kind is not Float32 or Float64. 996 func (v Value) OverflowFloat(x float64) bool { 997 return value_OverflowFloat(v, x) 998 } 999 1000 // OverflowInt reports whether the int64 x cannot be represented by v's type. 1001 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64. 1002 func (v Value) OverflowInt(x int64) bool { 1003 return value_OverflowInt(v, x) 1004 } 1005 1006 // OverflowUint reports whether the uint64 x cannot be represented by v's type. 1007 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64. 1008 func (v Value) OverflowUint(x uint64) bool { 1009 return value_OverflowUint(v, x) 1010 } 1011 1012 //go:nocheckptr 1013 // This prevents inlining Value.Pointer when -d=checkptr is enabled, 1014 // which ensures cmd/compile can recognize unsafe.Pointer(v.Pointer()) 1015 // and make an exception. 1016 1017 // Pointer returns v's value as a uintptr. 1018 // It returns uintptr instead of unsafe.Pointer so that 1019 // code using reflect cannot obtain unsafe.Pointers 1020 // without importing the unsafe package explicitly. 1021 // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer. 1022 // 1023 // If v's Kind is Func, the returned pointer is an underlying 1024 // code pointer, but not necessarily enough to identify a 1025 // single function uniquely. The only guarantee is that the 1026 // result is zero if and only if v is a nil func Value. 1027 // 1028 // If v's Kind is Slice, the returned pointer is to the first 1029 // element of the slice. If the slice is nil the returned value 1030 // is 0. If the slice is empty but non-nil the return value is non-zero. 1031 func (v Value) Pointer() uintptr { 1032 return value_Pointer(v) 1033 } 1034 1035 // Recv receives and returns a value from the channel v. 1036 // It panics if v's Kind is not Chan. 1037 // The receive blocks until a value is ready. 1038 // The boolean value ok is true if the value x corresponds to a send 1039 // on the channel, false if it is a zero value received because the channel is closed. 1040 func (v Value) Recv() (Value, bool) { 1041 return value_Recv(v) 1042 } 1043 1044 // Send sends x on the channel v. 1045 // It panics if v's kind is not Chan or if x's type is not the same type as v's element type. 1046 // As in Go, x's value must be assignable to the channel's element type. 1047 func (v Value) Send(x Value) { 1048 value_Send(v, x) 1049 } 1050 1051 // Set assigns x to the value v. 1052 // It panics if CanSet returns false. 1053 // As in Go, x's value must be assignable to v's type. 1054 func (v Value) Set(x Value) { 1055 value_Set(v, x) 1056 } 1057 1058 // SetBool sets v's underlying value. 1059 // It panics if v's Kind is not Bool or if CanSet() is false. 1060 func (v Value) SetBool(x bool) { 1061 value_SetBool(v, x) 1062 } 1063 1064 // SetBytes sets v's underlying value. 1065 // It panics if v's underlying value is not a slice of bytes. 1066 func (v Value) SetBytes(x []byte) { 1067 value_SetBytes(v, x) 1068 } 1069 1070 // SetCap sets v's capacity to n. 1071 // It panics if v's Kind is not Slice or if n is smaller than the length or 1072 // greater than the capacity of the slice. 1073 func (v Value) SetCap(n int) { 1074 value_SetCap(v, n) 1075 } 1076 1077 // SetComplex sets v's underlying value to x. 1078 // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false. 1079 func (v Value) SetComplex(x complex128) { 1080 value_SetComplex(v, x) 1081 } 1082 1083 // SetFloat sets v's underlying value to x. 1084 // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false. 1085 func (v Value) SetFloat(x float64) { 1086 value_SetFloat(v, x) 1087 } 1088 1089 // SetInt sets v's underlying value to x. 1090 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64, or if CanSet() is false. 1091 func (v Value) SetInt(x int64) { 1092 value_SetInt(v, x) 1093 } 1094 1095 // SetLen sets v's length to n. 1096 // It panics if v's Kind is not Slice or if n is negative or 1097 // greater than the capacity of the slice. 1098 func (v Value) SetLen(n int) { 1099 value_SetLen(v, n) 1100 } 1101 1102 // SetMapIndex sets the element associated with key in the map v to elem. 1103 // It panics if v's Kind is not Map. 1104 // If elem is the zero Value, SetMapIndex deletes the key from the map. 1105 // Otherwise if v holds a nil map, SetMapIndex will panic. 1106 // As in Go, key's elem must be assignable to the map's key type, 1107 // and elem's value must be assignable to the map's elem type. 1108 func (v Value) SetMapIndex(key, elem Value) { 1109 value_SetMapIndex(v, key, elem) 1110 } 1111 1112 // SetPointer sets the unsafe.Pointer value v to x. 1113 // It panics if v's Kind is not UnsafePointer. 1114 func (v Value) SetPointer(x unsafe.Pointer) { 1115 value_SetPointer(v, x) 1116 } 1117 1118 // SetString sets v's underlying value to x. 1119 // It panics if v's Kind is not String or if CanSet() is false. 1120 func (v Value) SetString(x string) { 1121 value_SetString(v, x) 1122 } 1123 1124 // SetUint sets v's underlying value to x. 1125 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64, or if CanSet() is false. 1126 func (v Value) SetUint(x uint64) { 1127 value_SetUint(v, x) 1128 } 1129 1130 // Slice returns v[i:j]. 1131 // It panics if v's Kind is not Array, Slice or String, or if v is an unaddressable array, 1132 // or if the indexes are out of bounds. 1133 func (v Value) Slice(i, j int) Value { 1134 return value_Slice(v, i, j) 1135 } 1136 1137 // Slice3 is the 3-index form of the slice operation: it returns v[i:j:k]. 1138 // It panics if v's Kind is not Array or Slice, or if v is an unaddressable array, 1139 // or if the indexes are out of bounds. 1140 func (v Value) Slice3(i, j, k int) Value { 1141 return value_Slice3(v, i, j, k) 1142 } 1143 1144 // String returns the string v's underlying value, as a string. 1145 // String is a special case because of Go's String method convention. 1146 // Unlike the other getters, it does not panic if v's Kind is not String. 1147 // Instead, it returns a string of the form "<T value>" where T is v's type. 1148 // The fmt package treats Values specially. It does not call their String 1149 // method implicitly but instead prints the concrete values they hold. 1150 func (v Value) String() string { 1151 return value_String(v) 1152 } 1153 1154 // TryRecv attempts to receive a value from the channel v but will not block. 1155 // It panics if v's Kind is not Chan. 1156 // If the receive delivers a value, x is the transferred value and ok is true. 1157 // If the receive cannot finish without blocking, x is the zero Value and ok is false. 1158 // If the channel is closed, x is the zero value for the channel's element type and ok is false. 1159 func (v Value) TryRecv() (Value, bool) { 1160 return value_TryRecv(v) 1161 } 1162 1163 // TrySend attempts to send x on the channel v but will not block. 1164 // It panics if v's Kind is not Chan. 1165 // It reports whether the value was sent. 1166 // As in Go, x's value must be assignable to the channel's element type. 1167 func (v Value) TrySend(x Value) bool { 1168 return value_TrySend(v, x) 1169 } 1170 1171 // Type returns v's type. 1172 func (v Value) Type() Type { 1173 return value_Type(v) 1174 } 1175 1176 // Uint returns v's underlying value, as a uint64. 1177 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64. 1178 func (v Value) Uint() uint64 { 1179 return value_Uint(v) 1180 } 1181 1182 //go:nocheckptr 1183 // This prevents inlining Value.UnsafeAddr when -d=checkptr is enabled, 1184 // which ensures cmd/compile can recognize unsafe.Pointer(v.UnsafeAddr()) 1185 // and make an exception. 1186 1187 // UnsafeAddr returns a pointer to v's data. 1188 // It is for advanced clients that also import the "unsafe" package. 1189 // It panics if v is not addressable. 1190 func (v Value) UnsafeAddr() uintptr { 1191 return value_UnsafeAddr(v) 1192 }