The reflection mechanism reflect in go language

Source: Internet
Author: User
Tags reflection

The reflection mechanism of go is to be carried out through the interface, and the null interface of object like Java can interact with any type, so reflection on the basic data type is also directly used in this feature.

Package Mainimport ("FMT" "reflect") type Student struct {Name string age int Sex UINT//0-female, 1-Male Address string}func (stu Student) Print () {sex: = "female" if Stu. Sex = = 1 {sex = "male"} fmt. Printf ("Name:%s, Age:%d, Gender:%s, Address:%s", Stu. Name, Stu. Age, sex, Stu. Address)}func (Stu Student) Say (content String) string{return FMT. Sprintf ("%s said:%s", Stu.    Name, content)}func main () {//============================================================ operation basic Data type var a1 int = 1 var a2 string = "China" var a3 = [...]     Byte{1, 2, 3}//array var a4 = []int{5, 6, 7}//Slice var A5 = map[string]int{"China": 1, "USA": 2}//Map Convert to reflect object, reflect. The data type returned by type and Reflect.value//TypeOf () is *reflect.rtype,valueof () and the data type returned is reflect. Value T1: = reflect.   TypeOf (A1); V1: = reflect. ValueOf (a1) T2: = reflect.   TypeOf (A2); V2: = reflect. ValueOf (A2) T3: = reflect.   TypeOf (A3); V3: = reflect. ValueOf (a3) T4: = reflect.   TypeOf (A3); V4: =Reflect. ValueOf (A4) T5: = Reflect.   TypeOf (A3); V5: = reflect. ValueOf (A5) fmt. Println ("a1====", T1, v1) fmt. Println ("a2====", T2, V2) fmt. Println ("a3====", T3, v3) fmt. Println ("a4====", T4, v4) fmt. Println ("a5====", T5, v5) fmt. PRINTLN ()//value FMT. Println ("A1 Value:", v1. Int ()) fmt. Println ("A2 Value:", V2. String ()) fmt. Println (the value of the element labeled 1 in A3: ", V3. Index (1)) fmt. Println (the value of the element labeled 1 in A4: ", v4. Index (1)) fmt. Println ("A4 [1,3) ' Sub-slice:", v4. Slice (1, 3)) Fmt. PRINTLN ("All key in A5:", v5. Mapkeys ()) fmt. Print ("Traverse value in A5:") for _, Key: = Range V5. Mapkeys () {fmt. Print (key, "= = =", v5. Mapindex (key), "\ T")} FMT. Println () fmt. PRINTLN ()//Get type FMT. Println ("A1 type:", v1. Type ()) fmt. Println ("A2 type:", V2. Type ()) fmt. Println ("A3 type:", V3. Type ()) fmt. Println ("A4 type:", v4. Type ()) fmt. Println ("A5 type:", V5. Type ()) fmt. PRINTLN ()//Kind type judgment, Kind () fmt. Println ("A1 is of type int:", v1. Kind () = = reflect. INT) fmt. Println ("The type of A2 is string:", V2. Kind () = = reflect. String) fmt. Println ("The type of A3 is array:", V3. Kind () = = reflect. Array) fmt. PRINTLN (whether the type of A4 is slice: ", v4. Kind () = = reflect. Slice) fmt. PRINTLN (whether the type of A5 is map: ", V5. Kind () = = reflect. MAP) fmt. PRINTLN ()///interface-type variable, interface is the value that gets the values, and returns a interface object, FMT. Printf ("a1====%t\t%v\n", V1. Interface (), v1. Interface ()) fmt. Printf ("a2====%t\t%v\n", V2. Interface (), V2. Interface ()) fmt. Printf ("a3====%t\t%v\n", V3. Interface (), V3. Interface ()) fmt. Printf ("a4====%t\t%v\n", V4. Interface (), V4. Interface ()) fmt. Printf ("a5====%t\t%v\n", V5. Interface (), V5. Interface ()) fmt. PRINTLN ()//Determine if FMT can be modified. Println ("A1 can be modified by reflection:", V1. Canset ()) fmt. Println ("A2 can be modified by reflection:", V2. Canset ()) fmt. Println ("A3 can be modified by reflection:", V3. Canset ()) fmt. Println ("A4 can be modified by reflection:", V4. Canset ()) fmt. Println ("A5 can be modified by reflection:", V5. Canset ()) fmt. PRINTLN ()//modify, must pass pointer, and call Elem () Vv1: = reflect. ValueOf (&AMP;A1). Elem () fmt. Printf ("VV1 type:%T,A1 can now be passed:%v\n", VV1, Vv1. Canset ()) Vv2: = reflect. ValueOf (&AMP;A2). Elem () Vv3: = reflect. ValueOf (&AMP;A3). Elem () Vv4: = reflect. ValueOf (&AMP;A4). Elem () Vv5: = reflect. ValueOf (&AMP;A5). Elem () vv1. Setint (100)//Modify VV2. SetString ("United States") var temp byte = Vv3. Index (0). Set (reflect. ValueOf (temp))//must pass reflect. The value type Vv4. Index (0). Setint (900)//must be transmitted reflect. The value type VV5. Setmapindex (reflect. ValueOf ("China"), reflect. ValueOf (111)) FMT. Println ("A1 Modified Value:", A1) Fmt. PRINTLN ("A2 Modified Value:", A2) fmt. PRINTLN ("A3 Modified Value:", A3) fmt. Println ("A4 Modified value:", A4) fmt. Println ("A5 Modified value:", A5)//============================================================ operator structure stu: = student{"John Doe", 18 , 1, "Tiananmen Square 10,000th, Beijing, China"}//converted into reflect object, reflect. Type and reflect.value st1: = reflect. TypeOf (stu) Sv1: = reflect. ValueOf (Stu) fmt. Println (st1, "= = =", SV1)//Get struct name FMT. Println (St1. Name ())//Judge kind type FMT. Println (St1. Kind () = = reflect. struct)//Gets the number of fields in the struct FMT. Println (St1. Numfield())//Gets the value of each field in the struct for I: = 0; I < St1. Numfield (); i++ {fieldName: = St1. Field (i). Name//Fetch field name FieldType: = St1. Field (i). Type fieldval: = Sv1. Field (i). Interface ()//value FMT. Printf ("Field name:%v, type:%v, Value:%v\n", FieldName, FieldType, Fieldval)}//Gets the number of methods of the struct FMT. Println (St1. Nummethod ())//traverse the name and type of all methods for I: = 0; I < St1. Nummethod (); i++ {method: = St1. Method (i) fmt. Println (method. Name, "= =", method. Type)}//Through reflection execution method M1: = Sv1. Methodbyname ("Print") M1. Call (nil)//without parameters and return value FMT. Println () M2: = Sv1. Methodbyname ("Say") Params: = []reflect. Value{reflect. ValueOf ("Hello! ")} Res: = m2. Call (params)//with parameters and return values, the parameter is reflect. The value of the slice, and the return values are the same as FMT. Println (Res[0]. String ())}

Reflection mechanism in the go language reflect

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.