In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "case Analysis of the reflection Mechanism of Go language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Because there is no experience in strongly typed languages, the concept of reflection has not been touched much before. I searched Wikipedia and explained it as follows:
In computer science, reflex programming (English: reflective programming) or reflex (English: reflection) refers to the ability of a computer program to access, detect and modify its own state or behavior at run time (runtime). Metaphorically, reflection is the ability of a program to "observe" and modify its own behavior while it is running.
The same is true of reflection in go, where you can get information about the type and value of a variable while the program is running, and then access or modify it. In the go language, there is a built-in reflect package that is used to get the type and value of a variable, corresponding to reflect.TypeOf () and reflect.ValueOf (), respectively.
Reflection type
The TypeOf method returns the type object of the variable, and the type and type of the variable can be obtained under the type object.
Import ("fmt"reflect") func main () {/ / define a variable of type int var i int = 1 / / get the type object of the variable var typeOfNum = reflect.TypeOf (I) / / output type and type typeOfNumName = typeOfNum.Name () typeOfNumKind = typeOfNum.Kind () fmt.Printf ("name:% s, kind:% s", typeOfNumName, typeOfNumKind)}
As you can see, the type and category at this time are int.
Type and category
The type represents the type specified when the variable is defined, which reflects the type defined by the type keyword, and the type is the type to which the variable ultimately belongs. It may be pale to say, let's go straight to the code.
Type num int / / defines a variable of type num var i num = 1 var typeOfNum = reflect.TypeOf (I)
As you can see, at this time the type is num and the category is int.
For some variables of reference types, such as slices, functions, and structures, kind can accurately reflect their underlying types.
Func printTypeOf (typeOf reflect.Type) {fmt.Printf ("name:% s, kind:% s\ n", typeOf.Name (), typeOf.Kind ()} type Person struct {} type IntSlice [] int func main () {var a = IntSlice {} var b = Person {} printTypeOf (reflect.TypeOf (a)) printTypeOf (reflect.TypeOf (b))}
For anonymous structures or anonymous functions, the type value is returned as empty.
Func main () {var a = struct {} {} printTypeOf (reflect.TypeOf (a))}
Reflection value
The ValueOf method, which can get the value of a variable.
Var I = 3.1415926 var s = "Welcome to my official account:" fmt.Println (reflect.ValueOf (s)) fmt.Println (reflect.ValueOf (I))
Through the reflected value object, we can also get the type of variable, and according to its type, we can call the corresponding method to obtain the real value of the variable.
Var I = 100 var v = reflect.ValueOf (I) fmt.Println (v.Int ()) / / if the value is of type Int, you can get the specific value fmt.Println (v.Kind ()) through the Int method
Modify valu
By reflecting the value object, you can modify the value of the variable itself. First of all, when you get the reflection value, you can't get the reflection value of the variable directly, but take the value object of its pointer first.
Var I = 100var v = reflect.ValueOf (& I) / / take out the value object fmt.Println (v.Kind (), v) of the pointer of variable I
After fetching the value object of the pointer, it cannot be assigned immediately, because what you get at this time is the address of the variable.
To assign a value, you need to call the Elem method, take out the specific element, and then assign it.
Var I = 100var v = reflect.ValueOf (& I) / take out the value object of the pointer of variable I var e = v.Elem () e.SetInt / / modify the element value fmt.Println (e.Kind (), I)
Value objects and structures
As mentioned earlier, the value of a variable can be obtained by reflection, and the same is true for structures.
Type Person struct {name string age int gender string address string} var p = Person {"Shenfq", 25, "male", "Hunan Changsha"} var v = reflect.ValueOf (p) fmt.Println (v.Kind (), v)
The reflection value object also provides some methods specifically for obtaining information about structure members.
NumField ()
NumField () can get the exact number of structure members.
Var p = Person {"Shenfq", 25, "male", "Hunan Changsha"} var v = reflect.ValueOf (p) fmt.Println ("number of members of Person structure:", v.NumField ())
Field ()
Field () can get the reflection value of the member at the specified index position of the structure.
Var p = Person {"Shenfq", 25, "male", "Hunan Changsha"} var v = reflect.ValueOf (p) var num = v.NumField () for I: = 0; I < num; ionization + {var val = v.Field (I) fmt.Printf ("Person [% d]:% s% v\ n", I, val.Type (), val)}
FieldByName ()
FieldByName () can get the reflection value of the member of the specified member name of the structure.
Var p = Person {"Shenfq", 25, "male", "Hunan Changsha"} var v = reflect.ValueOf (p) var vOfName = v.FieldByName ("name") fmt.Printf ("Person [name]:% s% v\ n", vOfName.Type (), vOfName)
This is the end of the content of "case Analysis of reflection Mechanism of Go language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.