golang 反射结构字段类型

来源:互联网 时间:1970-01-01

golang 反射用法,做个笔记.package mainimport ("fmt""reflect")type roles struct {roleId introleName string}type User struct {Name stringAge intEmail stringNickName stringTelphone intRoles roles}func main() {u := User{Name: "Name", Age: 30, Email: "[email protected]", NickName: "omni360", Telphone: xxxxx, Roles: roles{roleId: 1001, roleName: "administrator"}}fmt.Println(u)Info(u)}func Info(o interface{}) {t := reflect.TypeOf(o)fmt.Println("Type:", t.Name())v := reflect.ValueOf(o)fmt.Println("Fileds:")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)}}

版权声明:本文为博主原创文章,未经博主允许不得转载。



相关阅读:
Top