In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "what does go language reflection refer to". 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!
Interpretation of reflection
Reflection in Go is quite different from other languages. Emission in Golang mainly involves two basic concepts, Type and Value, which are also the two most important types in the reflect package of the Go language package.
By reflecting all interfaces in Golang, you can get an information structure containing Type and Value. As the name implies, Type mainly expresses the type information of the reflected variable itself, while Value is the information of the instance of the variable itself.
The role of reflection
Reflection in Golang has two main functions, namely, getting type information and getting value types.
With reflection, we can:
The encapsulation of dynamic analytic functions. Reflection acquisition data type
Syntax:
Reflect.TypeOf (x)
Function:
Get data type
Usage:
Package main
Import (
"fmt"
"reflect"
)
Func main () {
Var x = 3.4
Var str = "Hello World"
Fmt.Println ("x type =", reflect.TypeOf (x))
Fmt.Println ("str type =", reflect.TypeOf (str))
} reflection to get Type
Syntax:
Reflect.TypeOf (varname)
Function:
You can get the corresponding value of this variable.
Usage:
Package main
Import (
"fmt"
"reflect"
)
Func main () {
Var x = 1024
Var str = "Hello World"
Fmt.Println ("x type =", reflect.TypeOf (x))
Fmt.Println ("str type =", reflect.TypeOf (str))
}
Reflection acquisition Type
Syntax:
Reflect.TypeOf (varname) .Kind ()
Function:
Use reflect.TypeOf to pass in the variable we want to get, that is, we can get the type of the variable, and use the Kind method to get the details of the type.
Usage:
Package main
Import (
"fmt"
"reflect"
)
Func main () {
Var x = 1024
Var str = "Hello World"
TypeX: = reflect.TypeOf (x)
TypeStr: = reflect.TypeOf (str)
TypexKind: = typeX.Kind ()
TypeStrKind: = typeStr.Kind ()
Fmt.Println ("x type =", typeX, ", Kind =", typexKind)
Fmt.Println ("str type =", typeStr, ", Kind =", typeStrKind)
} reflection gets variable value information
Syntax:
Reflect.ValueOf (varname)
Function:
Using reflect.ValueOf to pass in the variable we want to get, we can get the value information of the variable.
Usage:
Package main
Import (
"fmt"
"reflect"
)
Func main () {
Var x = 1024
Var str = "Hello World"
ValueX: = reflect.ValueOf (x)
ValueStr: = reflect.ValueOf (str)
Fmt.Println ("valueX =", valueX)
Fmt.Println ("valueStr =", valueStr)
} reflection gets the pointer pointed to by the variable
Syntax:
Reflect.ValueOf (varname) .Elem ()
Function:
Using reflect.ValueOf to pass in the variable we want to get, we can get the value information of the variable.
Usage:
Package main
Import (
"fmt"
"reflect"
)
Func main () {
Var x = 1024
Var str = "Hello World"
ValueX: = reflect.ValueOf (x)
ValueStr: = reflect.ValueOf (str)
Fmt.Println ("valueX =", valueX)
Fmt.Println ("valueStr =", valueStr)
ValueElemX: = valueX.Elem ()
ValueElemStr: = valueStr.Elem ()
Fmt.Println ("valueElemX =", valueElemX)
Fmt.Println ("valueElemStr =", valueElemStr)
} reflection calls the structure method
Syntax:
PersonValue: = reflect.ValueOf (p)
InfoFunc: = personValue.MethodByName ("Info")
InfoFunc.Call ([] reflect.Value {})
Function:
Get the value information of the structure through reflect.ValueOf, and then use the MethodByName of the structure value information to obtain the structure. Finally, you can use the Call method to call the structure.
Usage:
Import (
"fmt"
"reflect"
)
Type Student struct {
Name string
Age int
Score float64
}
Func (s Student) Info () {
Fmt.Println ("Name =", s.Name, "Age =", s.Age, "Score =", s.Score)
}
Func main () {
Var p = Student {
Name: "Jim"
Age:10
Score:99
}
PersonValue: = reflect.ValueOf (p)
InfoFunc: = personValue.MethodByName ("Info")
InfoFunc.Call ([] reflect.Value {})
} combined use
The following code uses TypeOf to deal with the array and time type values contained in the structure. It can be used as a general reflection method.
Func reflect (o interface {}) error {
Re: = reflect.TypeOf (o) .Elem ()
Rv: = reflect.ValueOf (o) .Elem ()
/ / determine whether it is a structure or not
If re.Kind () = = reflect.Struct {
For I: = 0; I < re.NumField (); iTunes + {
F: = re.Field (I)
Name: = f.Name
Fmt.Printf ("field name% v:", name)
/ / get the value of one of the fields in the structure
V: = rv.FieldByName (name)
If v.Kind () = = reflect.Struct {
/ / processing time type
If v.Type () .ConvertibleTo (reflect.TypeOf (time.Time {})) {
Fmt.Printf ("field name:% v type of time", name)
Continue
}
/ / determine whether it is empty or not
If! v.IsNil () {
Fmt.Printf ("field name:% v is empty", name)
Continue
}
/ / TODO there is no business logic to supplement here
}
/ / handle array types
If v.Kind () = = reflect.Slice {
For j: = 0; j < v.Len (); Jake + {
/ / determine whether the objects in the array are pointer types
If reflect.TypeOf (v.Index (j). Interface ()) .Kind () = = reflect.Ptr {
Fmt.Printf ("field name:% v type of Ptr", name)
Continue
}
}
}
}
}
Return nil
}
This is the end of the content of "what does go language reflection refer to". 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.