Go: reflect
package main import ( "fmt" "reflect" ) type Vale struct { Name string `json:"name,omitempty"` Age int `json:"vale_age,omitempty"` Score float32 `json:"成绩"` Sex string } func (v Vale) GetSum(n1, n2 int) int { return n1 + n2 } func (v Vale) Set(name string, age int, score float32, sex string) { v.Name = name v.Age = age v.Score = score v.Sex = sex } func (v Vale) ShowInfo() { println("showinfo start") fmt.Println(v) println("showinfo stop") } func VailStruct(b interface{}) { typeOf := reflect.TypeOf(b) fmt.Printf("reflect.TypeOf() %T %v\n", typeOf, typeOf) valueOf := reflect.ValueOf(b) kind := valueOf.Kind() fmt.Printf("valueOf.kind() %T %v\n", kind, kind) if kind != reflect.Struct { fmt.Println("expect struct") return } numField := valueOf.NumField() fmt.Println("valueOf.NumField:", numField) for i := 0; i < numField; i++ { fmt.Printf("Field index: %v value: %v type: %T\n", i, valueOf.Field(i), valueOf.Field(i)) jsonTag := typeOf.Field(i).Tag.Get("json") if jsonTag != "" { fmt.Printf("Field index: %v jsonTag: %v\n", i, jsonTag) } numMethod := valueOf.NumMethod() fmt.Printf("valueOf.NumMethod(): %v\n", numMethod) valueOf.Method(2).Call(nil) // valueOf.Method(0).Call() var params []reflect.Value params = append(params, reflect.ValueOf(10)) params = append(params, reflect.ValueOf(40)) res := valueOf.Method(0).Call(params) fmt.Println("res:", res[0].Int()) } } func main() { var b Vale = Vale{ Name: "infinitesimal", Age: 55, Score: 88, } VailStruct(b) }
package main import ( "encoding/json" "fmt" "reflect" ) type Vale struct { Name string `json:"vale_name"` Age int Score float32 Sex string } func (v *Vale) ShowInfo() { println("~~~~~ShowInfo start~~~~~") fmt.Println(*v) println("~~~~~ShowInfo stop~~~~~") } func ReflectStruct(inter interface{}) { typeOf := reflect.TypeOf(inter) valueOf := reflect.ValueOf(inter) kind := valueOf.Kind() if kind != reflect.Ptr && valueOf.Elem().Kind() == reflect.Struct { fmt.Println("expect struct") return } numField := valueOf.Elem().NumField() valueOf.Elem().Field(0).SetString("Reflect_set") for i := 0; i < numField; i++ { fmt.Printf("index: %v, kind: %v\n", i, valueOf.Elem().Field(i).Kind()) } tag := typeOf.Elem().Field(0).Tag.Get("json") fmt.Println("tag:", tag) numMethod := valueOf.NumMethod() // 与方法绑定的类型对应 println("numMethod:", numMethod) valueOf.Method(0).Call(nil) // 与方法绑定的类型对应 } func main() { var vale Vale = Vale{ Name: "vale", Age: 55, Score: 99.9, } jsonString, _ := json.Marshal(vale) println("json:", jsonString) ReflectStruct(&vale) fmt.Println(vale) }
package main import ( "reflect" "testing" ) func TestReflectFunc(t *testing.T) { f1 := func(v1, v2 int) { t.Log(v1, v2) } f2 := func(v1, v2 int, s string) { t.Log(v1, v2, s) } var ( function reflect.Value inValue []reflect.Value n int ) adapter := func(f interface{}, args ...interface{}) { n = len(args) inValue = make([]reflect.Value, n) for i := 0; i < n; i++ { inValue[i] = reflect.ValueOf(args[i]) } function = reflect.ValueOf(f) function.Call(inValue) } adapter(f1, 5, 6) adapter(f2, 55, 66, "infinitesimal") }
package main import ( "reflect" "testing" ) type Moniker struct { Id int Name string } func TestReflectStruct(t *testing.T) { var ( model *Moniker value reflect.Value ) model = &Moniker{} value = reflect.ValueOf(model) t.Log("reflect.ValueOf", value.Kind().String(), value.String()) value = value.Elem() t.Log("reflect.ValueOf.Elem", value.Kind().String(), value) value.FieldByName("Id").SetInt(55) value.FieldByName("Name").SetString("moniker") t.Log(*model) }
package main import ( "reflect" "testing" ) type Moniker struct { Id int Name string } func TestReflectStructPtr(t *testing.T) { var ( model *Moniker reflectValue reflect.Value reflectType reflect.Type ) reflectType = reflect.TypeOf(model) // 获取类型 *Moniker t.Log("reflect.TypeOf", reflectType.Kind().String()) // ptr reflectType = reflectType.Elem() // ptr指向的类型 (struct) t.Log("reflect.TypeOf.Elem", reflectType.Kind().String()) // struct reflectValue = reflect.New(reflectType) // New返回一个reflectType类型值, 该值为一个ptr, 指向类型为reflectType的值(新建) t.Log("reflect.New", reflectValue.Kind().String()) // ptr t.Log("reflect.New.Elem", reflectValue.Elem().Kind().String()) // struct // model指向新建的Moniker结构体 model = reflectValue.Interface().(*Moniker) reflectValue = reflectValue.Elem() // 取得指针指向的值 reflectValue.FieldByName("Id").SetInt(55) reflectValue.FieldByName("Name").SetString("moniker") t.Log(model, model.Name) }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2021-05-20 centos6 阿里云 yun源