This is a creation in Article, where the information may have evolved or changed. Package Main
Import (
"FMT"
"Reflect"
)
Type person struct {
Name string
Age int
}
Func (P person) GetName () string {
Return P.name
}
Func (P person) SetName (s string) {
P.name = S
}
Func Main () {
A: = &person{"Name", 1}
T: = reflect. TypeOf (*a)//must be value, otherwise the type is empty
Fmt. Println (T.name ())
V: = reflect. ValueOf (a). Elem ()//a need to be a reference
K: = V.type ()
For I: = 0; I < V.numfield (); i++ {
Key: = K.field (i)
Val: = V.field (i)
Fmt. Println (key. Name, Val. Type (), Val. Interface ())
}
for I: = 0; i < V.nummethod (); i++ {
key: = K.method (i)
val: = V.method (i)
Span style= "White-space:pre" > FMT. Println (key. Name, Val. Type (), Val. Interface ())
}
v.fieldbyname (" Name "). Set (reflect. ValueOf ("Name"))
FMT. Println (a.name)
Name: = V.methodbyname ("GetName"). Call ([]reflect. value{})
FMT. PRINTLN (name)
}