"Go learning note" 13, reflective reflect

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Reflective Reflection

Reflection can greatly increase the flexibility of the program, so that interface{} has a greater scope of play
Reflection uses the TypeOf and valueof functions to get target object information from the interface
Reflection will use anonymous fields as separate fields (anonymous field nature)
You want to use reflection to modify the state of an object, if Interface.data is settable, which is pointer-interface
Methods can be called dynamically by reflection

Structural Body Reflection

12345678910111213141516171819202122232425262728293031323334353637383940
typeUserstruct{IdintNamestringAgeint} func (u User) Hello() {FMT. Println ("Hello World")} func Info(o interface{}) {t: = reflect. TypeOf (o)ifK: = T.kind (); K! = reflect. Struct {fmt. Println ("XXX")return}fmt. Println ("Type:", T.name ()) V: = reflect. ValueOf (o) fmt. Println ("Fields:") forI: =0; I < T.numfield (); i++ {f: = T.field (i) Val: = V.field (i). Interface () fmt. Printf ("%6s:%v =%v \ n", F.name, F.type, Val)} forI: =0; I < T.nummethod (); i++ {m: = T.method (i) fmt. Printf ("%6s:%v\n", M.name, M.type)}} func main() {u: = user{1,"OK", A}info (U)}

Calling Anonymous fields through reflection

 1234567891011121314151617 
 type  User struct  {Id int  Name string  age int } type  Manager struct  {usertitle string } func  main   ()   {m: = Manager{user:user{1 ,  "OK" , 19 }, Title: }t: = reflect. TypeOf (m) fmt. Printf ( "% #v \ n" , T.fieldbyindex ([]int  { 0 , 0 })} 

Modifying an object's state by reflection

Basic type

123456
func Main ()t: = reflect. ValueOf (&x) T.elem (). Setint (111) fmt. PRINTLN (x)}

Structural body

123456789101112131415161718192021222324252627282930313233
 func main() {u: = user{1,"OK", +}set (&u) fmt. PRINTLN (U)}typeUserstruct{IdintNamestringAgeint} func Set(o interface{}) {V: = reflect. ValueOf (o)ifV.kind () = = reflect. Ptr &&!v.elem (). Canset () {fmt. Println ("xxxx")return}Else{v = V.elem ()}f: = V.fieldbyname ("Name")if!f.isvalid () {fmt. Println ("Bad")return}ifF.kind () = = reflect. String {f.setstring ("Success")}}

Dynamic Call method by reflection

12345678910111213141516171819
 type  User struct  {Id int  Name string  age int } func   (U User)  hello  string )   {FMT. Println ( "Hello" , name, , u.name)}func  main   ()   {u: = User{1 , , 19 } V: = reflect. ValueOf (u) MV: = V.methodbyname ( "Hello" ) args: = []reflect. Value{reflect. ValueOf ( "hundred" )}mv. Call (args)} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.