package main
import (
"reflect"
"fmt"
"190112/pkg"
)
func main() {
var reverseInt func([]int) []int
BindFunc(&reverseInt,Reverse)
fmt.Println(reverseInt([]int{10,20,30}))
var reverseString func([]string) []string
BindFunc(&reverseString,Reverse)
fmt.Println(reverseString([]string{"x","y","z"}))
s:=pkg.Student{Name:"lily",Age:25}
sv:=reflect.ValueOf(s)
fmt.Println(sv.NumField())
for i:=0; i<sv.NumField(); i++ {
fmt.Println(sv.Field(i))
}
fmt.Println(sv.NumMethod())
sv.Method(0).Call([]reflect.Value{})
spv:=reflect.ValueOf(&s)
fmt.Println(spv.NumMethod())
spv.Method(0).Call([]reflect.Value{})
spv.Method(1).Call([]reflect.Value{reflect.ValueOf(60)})
fmt.Println(s)
var x1=10
t1:=reflect.TypeOf(x1)
fmt.Println(t1)
var x2 interface{}
x2=x1
t2:=reflect.TypeOf(x2)
fmt.Println(t2)
var x3="hello"
t3:=reflect.TypeOf(x3)
fmt.Println(t3)
var sl=[]int{1,2,3}
tsl:=reflect.TypeOf(sl)
fmt.Println(tsl)
fmt.Println(tsl.Elem())
st:=pkg.Student{Name:"mary",Age:10}
tst:=reflect.TypeOf(st)
fmt.Println(tst)
fmt.Println(tst.Kind())
fmt.Println(tst.Name())
fmt.Println(tst.NumField())
for i:=0; i<tst.NumField(); i++ {
f:=tst.Field(i)
fmt.Println(f.Name,f.Type)
t:=f.Tag
fmt.Println(t.Get("json"))
}
fmt.Println(tst.NumMethod())
stm:=tst.Method(0)
fmt.Println(stm.Name,stm.Type)
m:=map[string]string {
"m":"mm",
"n":"nn",
}
tm:=reflect.TypeOf(m)
fmt.Println(tm)
fmt.Println(tm.Kind(),tm.Name())
xv:=reflect.ValueOf(x1)
fmt.Println(xv)
fmt.Println(xv.CanSet())
xvp:=reflect.ValueOf(&x1)
fmt.Println(xvp)
fmt.Println(xvp.Elem(),xvp.Elem().Type())
fmt.Println(xvp.CanSet(),xvp.Elem().CanSet())
xvp.Elem().Set(reflect.ValueOf(30))
fmt.Println(x1)
slv:=reflect.ValueOf(sl)
fmt.Println(slv)
fmt.Println(slv.CanSet())
slv.Index(0).Set(reflect.ValueOf(10))
fmt.Println(sl)
slpv:=reflect.ValueOf(&sl)
fmt.Println(slpv)
fmt.Println(slpv.Elem(),slpv.Elem().Type())
fmt.Println(slpv.CanSet(),slpv.Elem().CanSet())
slpv.Elem().Index(0).Set(reflect.ValueOf(15))
fmt.Println(sl)
slpv.Elem().Set(reflect.ValueOf([]int{10,20,30}))
fmt.Println(sl)
fmt.Println(spv.Elem().NumField())
for i:=0; i<spv.Elem().NumField(); i++ {
f:=spv.Elem().Field(i)
fmt.Println(f)
t:=f.Type()
fmt.Println(t.Name(),t.Kind())
if t.Kind()==reflect.String {
spv.Elem().Field(i).Set(reflect.ValueOf("mary"))
}
}
fmt.Println(s)
}
func BindFunc(t interface{},f func([]reflect.Value) []reflect.Value) {
invert:=reflect.ValueOf(t).Elem()
invert.Set(reflect.MakeFunc(invert.Type(),f))
}
func Reverse(in []reflect.Value) []reflect.Value {
inslice,n:=in[0],in[0].Len()
outslice:=reflect.MakeSlice(inslice.Type(),0,n)
for i:=n-1; i>=0; i-- {
outslice=reflect.Append(outslice,inslice.Index(i))
}
return []reflect.Value{outslice}
}