1. reflect遍历结构体的属性和方法
1.1 具体实现
package main
import (
"fmt"
"reflect"
)
type User struct {
Id int
Name string
Age int
}
func (u User) Hello() {
fmt.Println("Hello World!")
}
func main() {
u := User{1, "Ok", 19}
Info(u)
}
func Info(o interface{}) {
t := reflect.TypeOf(o)
fmt.Println("Type: ", t.Name())
//判断是否是结构体类型
if k := t.Kind(); k != reflect.Struct {
fmt.Println("Not supported type")
return
}
v := reflect.ValueOf(o)
fmt.Println("Fields:")
//获取所有的字段
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
val := v.Field(i).Interface()
fmt.Printf("%6s: %v = %v\n", f.Name, f.Type, val)
}
//获取所有的方法
for i := 0; i < t.NumMethod(); i++ {
m := t.Method(i)
fmt.Printf("%6s: %v\n", m.Name, m.Type)
}
}
2. 修改变量的值
2.1 具体实现
package main
import (
"fmt"
"reflect"
)
func main() {
x := 123
v := reflect.ValueOf(&x)
v.Elem().SetInt(999)
fmt.Println(x)
}
2.2 具体实现2
package main
import (
"fmt"
"reflect"
)
type User struct {
Id int
Name string
Age int
}
func main() {
u := User{1, "ok", 198}
Set(&u)
fmt.Println(u)
}
func Set(o interface{}) {
v := reflect.ValueOf(o)
if v.Kind() == reflect.Ptr && !v.Elem().CanSet() {
fmt.Println("cannot set")
return
} else {
v = v.Elem()
}
f := v.FieldByName("Name")
if !f.IsValid() {
fmt.Println("bad")
return
}
if f.Kind() == reflect.String {
f.SetString("byebye")
}
}
3. 通过反射动态调用方法
3.1 具体实现
package main
import (
"fmt"
"reflect"
)
type User struct {
Id int
Name string
Age int
}
func (u User) Hello(name string) {
fmt.Println("Hello", name, ". my name is ", u.Name)
}
func main() {
u := User{1, "ok", 198}
v := reflect.ValueOf(u)
mv := v.MethodByName("Hello")
args := []reflect.Value{reflect.ValueOf("joe")}
mv.Call(args)
}