github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/testdata/structreflect.go (about)

     1  // +build ignore
     2  
     3  package main
     4  
     5  import "reflect"
     6  
     7  type A struct {
     8  	f *int
     9  	g interface{}
    10  	h bool
    11  }
    12  
    13  var dyn string
    14  
    15  func reflectTypeFieldByName() {
    16  	f, _ := reflect.TypeOf(A{}).FieldByName("f")
    17  	print(f.Type) // @pointsto *int
    18  
    19  	g, _ := reflect.TypeOf(A{}).FieldByName("g")
    20  	print(g.Type)               // @pointsto interface{}
    21  	print(reflect.Zero(g.Type)) // @pointsto <alloc in reflect.Zero>
    22  	print(reflect.Zero(g.Type)) // @types interface{}
    23  
    24  	print(reflect.Zero(g.Type).Interface()) // @pointsto
    25  	print(reflect.Zero(g.Type).Interface()) // @types
    26  
    27  	h, _ := reflect.TypeOf(A{}).FieldByName("h")
    28  	print(h.Type) // @pointsto bool
    29  
    30  	missing, _ := reflect.TypeOf(A{}).FieldByName("missing")
    31  	print(missing.Type) // @pointsto
    32  
    33  	dyn, _ := reflect.TypeOf(A{}).FieldByName(dyn)
    34  	print(dyn.Type) // @pointsto *int | bool | interface{}
    35  }
    36  
    37  func reflectTypeField() {
    38  	fld := reflect.TypeOf(A{}).Field(0)
    39  	print(fld.Type) // @pointsto *int | bool | interface{}
    40  }
    41  
    42  func main() {
    43  	reflectTypeFieldByName()
    44  	reflectTypeField()
    45  }