反射初識(shí)
package main
import (
"fmt"
"reflect"
)
//23
func main() {
/*
反射操作:通過(guò)反射刁品,可以獲取一個(gè)接口類型變量的 類型和數(shù)值
*/
var x float64 = 3.14
fmt.Println("type:",reflect.TypeOf(x)) //type: float64
fmt.Println("value:",reflect.ValueOf(x)) //value: 3.14
//根據(jù)反射的值,來(lái)獲取對(duì)應(yīng)的類型和數(shù)值
v := reflect.ValueOf(x)
fmt.Println("kind is float64:",v.Kind() == reflect.Float64) //kind is float64: true
fmt.Println("type:",v.Type()) //type: float64
fmt.Println("value:",v.Float()) //value: 3.14
}
reflect對(duì)象獲取接口變量信息
package main
import (
"fmt"
"reflect"
)
//24
func main() {
/*
反射操作:通過(guò)反射浩姥,可以獲取一個(gè)接口類型變量的 類型和數(shù)值
*/
var num float64 = 1.23
value := reflect.ValueOf(num)
convertValue := value.Interface().(float64)
fmt.Println(convertValue) //1.23
/*
反射類型對(duì)象 --> 接口類型變量挑随,理解為"強(qiáng)制轉(zhuǎn)換"
Golang對(duì)類型要求非常嚴(yán)格,類型一定要完全符合
一個(gè)是*float64勒叠,一個(gè)是float64兜挨,混淆就會(huì)panic
*/
pointer := reflect.ValueOf(&num)
convertpointer := pointer.Interface().(*float64)
fmt.Println(convertpointer) //0xc0000b4008
p1 := Person{"王二狗",18,"男"}
GetMessage(p1)
}
func GetMessage(input interface{}){
getType := reflect.TypeOf(input)
fmt.Println("get Type is : ",getType.Name()) //get Type is : Person
fmt.Println("get Kind is : ",getType.Kind()) //get Kind is : struct
getValue := reflect.ValueOf(input)
fmt.Println("get all Fields is :",getValue) //get all Fields is : {王二狗 18 男}
//獲取字段
/*
1。 獲取Type對(duì)象:reflect.Type
NumField()
Field(index)
2眯分。通過(guò)Field()獲得每一個(gè)Field字段
3拌汇。 Interface(),得到對(duì)應(yīng)的Value
*/
for i := 0; i < getType.NumField(); i++ {
field := getType.Field(i)
value := getValue.Field(i).Interface()
//字段名稱:Name,字段類型:string弊决,字段數(shù)值:王二狗
//字段名稱:Age,字段類型:int噪舀,字段數(shù)值:18
//字段名稱:Sex,字段類型:string,字段數(shù)值:男
fmt.Printf("字段名稱:%s,字段類型:%s飘诗,字段數(shù)值:%v\n",field.Name,field.Type,value)
}
//獲取方法
for i := 0; i < getType.NumMethod(); i++ {
method := getType.Method(i)
//方法名稱:PrintInfo与倡,方法類型:func(main.Person)
//方法名稱:Say,方法類型:func(main.Person, string)
fmt.Printf("方法名稱:%s昆稿,方法類型:%v\n",method.Name,method.Type)
}
}
type Person struct {
Name string
Age int
Sex string
}
func (p Person) Say(msg string) {
fmt.Println("hello",msg)
}
func (p Person) PrintInfo() {
fmt.Println("姓名:%s纺座,年齡:%d,性別:%s\n",p.Name,p.Age,p.Sex)
}
reflect對(duì)象設(shè)置實(shí)際變量的值
package main
import (
"fmt"
"reflect"
)
//25
func main() {
/*
反射操作:通過(guò)反射溉潭,可以獲取一個(gè)接口類型變量的 類型和數(shù)值
*/
var num float64 = 1.23
//需要操作指針
//通過(guò)reflect.ValueOf() 獲得num的value對(duì)象
pointer := reflect.ValueOf(&num) //注意參數(shù)必須是指針才能修改其值
newValue := pointer.Elem()
fmt.Println("類型:",newValue.Type()) //類型: float64
fmt.Println("是否可以修改:",newValue.CanSet()) //是否可以修改: true
//重新賦值
newValue.SetFloat(3.14)
fmt.Println(num) //3.14
s1 := Student{"suntong",22}
fmt.Printf("%T\n",s1) //main.Student
//反射改變數(shù)值
value := reflect.ValueOf(&s1)
if value.Kind() == reflect.Ptr {
newValue2 := value.Elem()
fmt.Println(newValue2) //{suntong 22}
f1 := newValue2.FieldByName("Name")
f1.SetString("zhangsan")
fmt.Println(s1) //{zhangsan 22}
}
}
type Student struct {
Name string
Age int
}
reflect對(duì)象進(jìn)行方法的調(diào)用
package main
import (
"fmt"
"reflect"
"strconv"
)
//26
func main() {
/*
通過(guò)反射來(lái)進(jìn)行方法的調(diào)用
思路:
1净响。 接口變量 -> 對(duì)象反射對(duì)象:Value
2. 獲取對(duì)應(yīng)的方法對(duì)象,MethodByName()
3. 將方法對(duì)象進(jìn)行調(diào)用喳瓣,Call()
*/
p1 := Person2{"suntong",22,"男"}
value := reflect.ValueOf(p1)
fmt.Printf("kind:%s,type:%s\n",value.Kind(),value.Type()) //kind:struct,type:main.Person2
methodValue1 := value.MethodByName("PrintInfo")
//kind:func,type:func()
fmt.Printf("kind:%s,type:%s\n",methodValue1.Kind(),methodValue1.Type())
//沒(méi)有參數(shù)馋贤,進(jìn)行調(diào)用
methodValue1.Call(nil) //姓名:suntong,年齡:22畏陕,性別:男
args1 := make([]reflect.Value,0) //空切片
methodValue1.Call(args1) //姓名:suntong刽漂,年齡:22酥泛,性別:男
methodValue2 := value.MethodByName("Say")
// kind:func,type:func(string)
fmt.Printf("kind:%s,type:%s\n",methodValue2.Kind(),methodValue2.Type())
args2 := []reflect.Value{reflect.ValueOf("反射機(jī)制")} //多個(gè)參數(shù)瓤漏,增加reflect.ValueOf(int/string。堤撵。。)
methodValue2.Call(args2) //hello 反射機(jī)制
//函數(shù)的反射
f5 := fun5
value1 := reflect.ValueOf(f5)
fmt.Printf("kind:%s,type:%s\n",value1.Kind(),value1.Type()) //kind:func,type:func()
value2 := reflect.ValueOf(fun6)
value3 := reflect.ValueOf(fun7)
fmt.Printf("kind:%s,type:%s\n",value2.Kind(),value2.Type()) //kind:func,type:func(int, string)
fmt.Printf("kind:%s,type:%s\n",value3.Kind(),value3.Type()) //kind:func,type:func(int, string) string
//通過(guò)反射調(diào)用函數(shù)
//fun5,無(wú)參
//fun6,有參
//fun5,有參有返回值
value1.Call(nil)
value2.Call([]reflect.Value{reflect.ValueOf(1000),reflect.ValueOf("suntong")})
resultValue := value3.Call([]reflect.Value{reflect.ValueOf(2000),reflect.ValueOf("isuntong")})
fmt.Printf("%T\n",resultValue) //[]reflect.Value
fmt.Println(len(resultValue)) //1
fmt.Printf("kind:%s,type:%s\n",resultValue[0].Kind(),resultValue[0].Type()) //kind:string,type:string
s := resultValue[0].Interface().(string)
fmt.Println(s) //isuntong2000
fmt.Printf("%T\n",s) //string
}
func fun5(){
fmt.Println("fun5,無(wú)參")
}
func fun6(i int,s string){
fmt.Println("fun6,有參")
}
func fun7(i int,s string)(string){
fmt.Println("fun5,有參有返回值")
return s + strconv.Itoa(i)
}
type Person2 struct {
Name string
Age int
Sex string
}
func (p Person2) Say(msg string) {
fmt.Println("hello",msg)
}
func (p Person2) PrintInfo() {
fmt.Printf("姓名:%s羽莺,年齡:%d实昨,性別:%s\n",p.Name,p.Age,p.Sex)
}