package main

import (
        "fmt"
        "reflect"
)

type User struct  {
        Id int
        Name string
        //addr string
}

func main(){
        u := User{Id:1001, Name:"xxx"/*, addr:"xxx"*/}
        t := reflect.TypeOf(u)
        v := reflect.ValueOf(u)
        for k := 0; k < t.NumFiled(); k++ {
                fmt.Printf("%s -- %v \n", t.Filed(k).Name, v.Field(k).Interface())   
        }
}


注:当结构体中含有非导出字段时,v.Field(k).Interface()会panic

相关文章:

  • 2021-05-20
  • 2021-11-06
  • 2021-07-26
  • 2021-11-15
  • 2022-12-23
  • 2022-01-06
猜你喜欢
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2021-06-17
  • 2022-01-14
  • 2022-01-26
相关资源
相似解决方案