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