In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use reflection in Go language, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
Overview
Access and modify the program dynamically while the program is running
Reflect godoc: https://golang.org/pkg/reflect/
The reflect package has two data types:
Type: data type [reflect.TypeOf (): is the method to get Type]
Value: type of value [reflect.ValueOf (): is the method to get Value]
Grammar 1. Basic operation
Get variable type
Func TypeOf (I interface {}) Type / / Type is an alias for interface {}
Examples
Reflect.TypeOf (10) / / intreflect.TypeOf (struct {age int} {10}) / / struct {age int}
Get the type of variable
Reflect.TypeOf (struct {age int} {10}). Kind () / / reflect.Structreflect.ValueOf ("hello word"). Kind () / / reflect.String
Get the value of the variable
Func ValueOf (I interface {}) Value / / value is an alias for struct {}
Examples
Reflect.ValueOf ("hello word") / / hello wordreflect.ValueOf (struct {age int} {10}) / / {10} II. Modify the target object
Modify normal type
Str: = "hello word" reflect.ValueOf (& str). Elem (). SetString (Zhang San)
Modify the structure
/ / first step: ValueOf (): pass in the address of a variable Return the address of the variable Elem (): return the original value of the variable elem:=reflect.ValueOf (& variable name). Elem () / second step FieldByName (): pass in the structure field name SetString (): pass in the variable value you want to modify elem.FieldByName ("Name"). SetString ("Li Si") / / define a User structure type User struct {Name string Age int} user: = User {Name: "Zhang San" Age: 10} / / Elem () get the original value of user elem: = reflect.ValueOf (& user). Elem () / / FieldByName () returns the structure field with the given name through Name and modifies the original value elem.FieldByName ("Name") through SetString. SetString ("Li Si") elem.FieldByName ("Age") .SetInt (18) III. Dynamic calling method
No-parameter method
/ / MethodByName (): pass the name of the square Method name must be Call (): method parameter reflect.ValueOf (variable name). MethodByName (method name). Call ([] reflect.Value {}) reflect.ValueOf (variable name). MethodByName (method name) .Call (make ([] reflect.Value) 0) type User struct {Name string `json: "name" name: "Zhang San" `Age int} func (_ User) Say () {fmt.Println ("user talk")} user: = User {Name: "Zhang San", Age: 10} reflect.ValueOf (& user) .MethodByName ("Say"). Call ([] reflect.Value {}) reflect.ValueOf (user) .MethodByName ("Say") .Call make ([] reflect.Value, 0))
Parametric method
Reflect.ValueOf (variable name). MethodByName (method name). Call ([] reflect.Value {reflect.ValueOf ("time to speak"), reflect.ValueOf (1)}) type User struct {Name string `json: "name" name: "Zhang San" `Age int} func (_ User) Say () {fmt.Println ("user talk")} user: = User {Name: "Zhang San" Age: 10} reflect.ValueOf (user) .MethodByName ("SayContent"). Call ([] reflect.Value {reflect.ValueOf ("it's time to talk"), reflect.ValueOf (1)}) Summary
Methods that reflect calls to struct must be public
Nil or [] reflect.Value {} is required when reflection calls a no-parameter method.
Example package mainimport ("fmt"reflect") func main () {/ / 1. Get variable type fmt.Println ("get variable type") fmt.Println (reflect.TypeOf (10)) / / int fmt.Println (reflect.TypeOf (10.0)) / / float64 fmt.Println (reflect.TypeOf (struct {age int} {10})) / / struct {age int} Fmt.Println (reflect.TypeOf (map [string] string {"a": "a"})) / / map [string] string fmt.Println (") / / 2. Get variable value fmt.Println ("get variable value") fmt.Println (reflect.ValueOf ("hello word")) / / hello word fmt.Println (reflect.ValueOf (struct {age int} {10})) / / {10} fmt.Println (reflect.TypeOf (struct {age int} {10}). Kind ()) / / struct / / Type decision Break if t: = reflect.TypeOf (struct {age int} {10}) .Kind () T = = reflect.Struct {fmt.Println ("is a structure")} else {fmt.Println ("not a structure")} / / modify the target object str: = "hello word" / / ordinary variable modification reflect.ValueOf (& str) .Elem () .SetString ("Zhang San") fmt .Println (str) / / structure variable modification user: = User {Name: "Zhang San" Age: 10} / / Elem () gets the original value of user elem: = reflect.ValueOf (& user). Elem () / / FieldByName () returns the structure field with the given name via Name and modifies the original value elem.FieldByName ("Name") by SetString. SetString ("Li Si") elem.FieldByName ("Age"). SetInt (18) fmt.Println ( User) / / gets the value of the tag of the structure fmt.Println (reflect.TypeOf (& user). Elem (). Field (0). Tag.Get ("name")) / / call the no-parameter method reflect.ValueOf (& user) .MethodByName ("Say"). Call ([] reflect.Value {}) reflect.ValueOf (user) .MethodByName ("Say") .Call (make ([] reflect.Value) )) / / call the parametric method reflect.ValueOf (user) .MethodByName ("SayContent"). Call ([] reflect.Value {reflect.ValueOf ("it's time to talk"), reflect.ValueOf (1)}) / / call the local method reflect.ValueOf (Hello). Call ([] reflect.Value {}) reflect.ValueOf (Hello) .call (nil) fmt.Printf ("% # v\ n") Reflect.TypeOf (user) .Field (0)} func Hello () {fmt.Println ("hello")} type Person struct {Name string} type User struct {Person / reflection processes Name string `field as a separate field: "name" name: "Zhang San" `Age int} func (_ User) Say () { Fmt.Println ("user talk")} func (_ User) SayContent (content string An int) {fmt.Println ("user", content, a)} these are all the contents of the article "how to use reflection in Go" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.