fmt.Printf("%#v\n", t.FieldByIndex([]int{0,0})) } funcInfo(iinterface{}) { t := reflect.TypeOf(i) ift.Kind() != reflect.Struct { fmt.Println("Type Error") return } fmt.Println("Type:", t.Name()) v := reflect.ValueOf(i) // get fields fmt.Println("Fields:") fori...
type reflectType struct { KindreflectKind Namestring PkgPathstring Fields[]reflectType Methods[]reflectMethod } //every go value(with any go type) can abstact as areflectValue type reflectValue struct { TypereflectType ValueunsafePointer Fields[]reflectStructField SpecificValueOpers() } //---an...
reflect.Value是通过reflect.ValueOf(x)获得的,只有当x是指针的时候,才可以通过reflec.Value修改实际变量x的值。 // Set assigns x to the value v. It panics if Value.CanSet returns false. // As in Go, x's value must be assignable to v's type and must not be derived from an unexported ...
StructTag 是结构体字段的标签。 type StructTag string Get 方法返回标签字符串中键 key 对应的值。如果标签中没有该键,会返回 ""。 func(tag StructTag)Get(key string)string package main import("fmt""reflect")consttagName="Testing"type Test struct{Name string `json:"-"`//...
{field:=getType.Field(i)value:=getValue.Field(i).Interface()fmt.Printf("%s: %v = %v\n",field.Name,field.Type,value)}// 获取方法// 1. 先获取interface的reflect.Type,然后通过.NumMethod进行遍历fori:=0;i<getType.NumMethod();i++{m:=getType.Method(i)fmt.Printf("%s: %v\n",m.Name...
case reflect.Int64: fmt.Printf("a is int64\n") case reflect.String: fmt.Printf("a is string\n") } } func reflect_value(a interface{}) { v := reflect.ValueOf(a) // t := reflect.TypeOf(a) k := v.Kind() //fmt.Printf("a store value is :%d\n", v.Int()) ...
<itype.NumField();i++{field:=itype.Field(i)value:=ivalue.Field(i).Interface()fmt.Printf("%s: %v = %v\n",field.Name,field.Type,value)}// 获取方法fmt.Println("reflect get methods:")fori:=0;i<itype.NumMethod();i++{m:=itype.Method(i)fmt.Printf("%s: %v\n",m.Name,m.Type)...
packagemainimport("fmt""reflect""testing""time")typeStustruct{StrstringTime time.Time}funcTestTime(t*testing.T){stu:=Stu{Str:"test",Time:time.Now()}print(stu)}funcprint(tinterface{}){getType:=reflect.TypeOf(t)getValue:=reflect.ValueOf(t)fori:=0;i<getType.NumField();i++{field:=getT...
typeStudentstruct{ Namestring Ageint } funcreflectTest02(binterface{}){ rTyp:=reflect.TypeOf(b) fmt.Println("rTyp=",rTyp) rVal:=reflect.ValueOf(b) iV:=rVal.Interface() fmt.Printf("iv=%v iv type=%T\n",iV,iV) student,ok:=iV.(Student) ...
intType:=reflect.Int typeName:=intType.String()// >>> int 结构体类型描述 type StructField 结构体字段信息 typeStructFieldstruct{NamestringPkgPathstring// 非导出字段包引入地址Type Type// 字段的类型Tag StructTag// 字段的标签Offsetuintptr// 字段在结构体中的字节偏移量Index[]int// 用于Type.Field...