🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 应用示例 ~~~ type Student struct { name string `json:"name"` age int `json:"age"` weight float64 `json:"wgh"` } func (stu *Student) Study(book string) { fmt.Println("study book:", book) } func TestReflectObject(t *testing.T) { stu := Student{ name: "Jim", age: 24, weight: 64.5, } name := reflect.ValueOf(stu).FieldByName("name").String() t.Log("stu name:", name) age := reflect.ValueOf(stu).FieldByName("age").Int() t.Log("stu age:", age) weight := reflect.ValueOf(stu).FieldByName("weight").Float() t.Log("stu weight:", weight) t.Log("===========field======") field, _ := reflect.TypeOf(stu).FieldByName("name") t.Log("field.Name:", field.Name) t.Log("field.Index:", field.Index) t.Log("field.PkgPath:", field.PkgPath) t.Log("field.Type:", field.Type) t.Log("field.Offset:", field.Offset) t.Log("field.Anonymous:", field.Anonymous) // 获取tag t.Log("field.Tag:", field.Tag.Get("json")) t.Log("===========method======") method := reflect.ValueOf(&stu).MethodByName("Study") method.Call([]reflect.Value{reflect.ValueOf("English")}) } ~~~