@@ -10,6 +10,7 @@ import (
10
10
"internal/goarch"
11
11
"internal/itoa"
12
12
"internal/unsafeheader"
13
+ "iter"
13
14
"math"
14
15
"runtime"
15
16
"unsafe"
@@ -2620,6 +2621,47 @@ func (v Value) UnsafePointer() unsafe.Pointer {
2620
2621
panic (& ValueError {"reflect.Value.UnsafePointer" , v .kind ()})
2621
2622
}
2622
2623
2624
+ // Fields yields each field of v, along with its description.
2625
+ //
2626
+ // It panics if v's Kind is not Struct.
2627
+ //
2628
+ // The i'th field yielded by Fields is the same as [Type.Field(i)] and [Value.Field(i)].
2629
+ func (v Value ) Fields () iter.Seq2 [StructField , Value ] {
2630
+ return func (yield func (StructField , Value ) bool ) {
2631
+ rtype := v .Type ()
2632
+ for i := range v .NumField () {
2633
+ if ! yield (rtype .Field (i ), v .Field (i )) {
2634
+ return
2635
+ }
2636
+ }
2637
+ }
2638
+ }
2639
+
2640
+ // Methods yields a function value corresponding to each method of v,
2641
+ // along with a description of the method.
2642
+ //
2643
+ // The arguments to a Call on the yielded function value should not include a receiver;
2644
+ // the returned function will always use v as the receiver. Note that [Method.Type]
2645
+ // in each yielded [Method] does include the receiver, and thus is not the same as
2646
+ // [Value.Type].
2647
+ //
2648
+ // Methods panics if v is a nil interface value.
2649
+ //
2650
+ // The i'th method yielded by Methods is the same as [Type.Method(i)] and [Value.Method(i)].
2651
+ //
2652
+ // Calling this method will force the linker to retain all exported methods in all packages.
2653
+ // This may make the executable binary larger but will not affect execution time.
2654
+ func (v Value ) Methods () iter.Seq2 [Method , Value ] {
2655
+ return func (yield func (Method , Value ) bool ) {
2656
+ rtype := v .Type ()
2657
+ for i := range v .NumMethod () {
2658
+ if ! yield (rtype .Method (i ), v .Method (i )) {
2659
+ return
2660
+ }
2661
+ }
2662
+ }
2663
+ }
2664
+
2623
2665
// StringHeader is the runtime representation of a string.
2624
2666
// It cannot be used safely or portably and its representation may
2625
2667
// change in a later release.
@@ -3221,8 +3263,8 @@ func (v Value) Comparable() bool {
3221
3263
return v .IsNil () || v .Elem ().Comparable ()
3222
3264
3223
3265
case Struct :
3224
- for i := 0 ; i < v . NumField (); i ++ {
3225
- if ! v . Field ( i ) .Comparable () {
3266
+ for _ , value := range v . Fields () {
3267
+ if ! value .Comparable () {
3226
3268
return false
3227
3269
}
3228
3270
}
0 commit comments