The reflect of "the.go.programming.language.2015.11"

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

      • Determine the type of reflectvalue and extract the data
      • Addressable
      • Gets the name of the struct method

Judge reflect. Value type and extract data

for reflect. Value type data, judged by kind ()

Func Formatatom (v reflect. Value) string {Switch V. Kind() {case reflect. Invalid: Return"Invalid"Case reflect. Int, reflect. IntReflect,. IntReflect,. Int8, reflect. Int64:return StrConv. Formatint(V. Int(),Ten) Case reflect. Bool: Return StrConv. Formatbool(V. Bool()) Case reflect. String: Return V. String() Default:return"No ex"}}

For other complex types of data

Func display (path string, v reflect. Value) {Switch V. Kind() {case reflect. Invalid: FMT. Println("") Case reflect. Slice, reflect. Array: For I: =0; i < V.len (); i++ {Display (FMT. Sprintf("%s[%d]", path, i), V. Index(i))} case reflect. Struct: For I: =0; i < V.numfield (); i++ {Fieldpath: = FMT. Sprintf("%s[%s]", Path, V. Type(). Field(i). Name) display (Fieldpath, V. Field(i))} case reflect. Map: For _, Key: = Range V. Mapkeys() {Display (FMT. Sprintf("[%s][%s]", Path, Formatatom (key)), V. Mapindex(key)) } default:fmt. Printf("%s=%s\n", Path, Formatatom (v))}}

Note that for structs using v.Type().Field(i).Name get domain names, take advantage of v.Field(i) getting domain values.

Addressable

Use CANADDR () members to determine whether addressable.
The following variables d are addressable and can be used d.Set(reflect.ValueOf(11)) to assign values.
You can also use d.Addr().Interface().(*int) the pointer to set the value, or you can take advantage of the d.SetInt(99) assignment:

func Addressable_main(){  x := 2 d: = reflect.  ValueOf (&x). Elem () FMT. Println (d.canaddr ()) D.Set (reflect.  ValueOf (one)) FMT. Println (x) DP: = d.Addr (). Interface (). (*int) *DP =  fmt. Println (x) D.setint (in) FMT. Println (x)   }

As follows: For variables of type int x , use rx.SetString("hello") causes panic. Variables of type interface{} can be used y or used ry.Set(reflect.ValueOf(3)) ry.Set(reflect.ValueOf("hello")) , but use can ry.SetInt(2) cause panic.

Func addressable_main2 () {x:=2Rx: = Reflect. ValueOf(&x). Elem() Rx. Setint(2) Rx. Set(Reflect. ValueOf(3))//Rx. SetString("Hello")//pannic varyinterface{} ry: = Reflect. ValueOf(&y). Elem() Ry. Set(Reflect. ValueOf(3)) Ry. Set(Reflect. ValueOf("Hello") FMT. Println(y)//Ry. Setint(2)//panic}func addressable_main3 () {stdout: = Reflect. ValueOf(OS. Stdout). Elem() FMT. Println(stdout. Type()) FD: = stdout. Fieldbyname("FD") FMT. Println(FD. Int())}

Gets the name of the struct method

The following code can see V as reflect. Variable of type value
Can be passed v.Method(i).Type().String() andv.Type().Method(i).Name()

func printmethodname (x  interface{}) {V: = reflect< Span class= "Hljs-preprocessor". ValueOf (x ) VT: = V () fmt ( "type%s \ n" , VT) for I: = 0   Methtype: = V (i)  () fmt (, VT, Vt (i) , Strings (Methtype (), )}} 
Related Article

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.