dongpin3794 2018-10-08 12:42
浏览 36
已采纳

递归扩展包含指针的结构定义

This question based on following: go reflection deeply in struct I need same thing - expand struct definition to pass it then as JSON object but with only difference that struct contains pointers to another structs. So, provided code unable to handle that. I tried to modify it in following way:

func printFields(prefix string, t reflect.Type) {
    for i := 0; i < t.NumField(); i++ {
        f := t.Field(i)
        fmt.Printf("%v%v %v
", prefix, f.Name, f.Type)
        if f.Type.Kind() == reflect.Struct {
            fmt.Println(reflect.New(f.Type))
            printFields(fmt.Sprintf("  %v", prefix), f.Type)
        } else if f.Type.Kind() == reflect.Ptr {
            fmt.Println("type ", f.Type )
            printFields(fmt.Sprintf("  %v", prefix), f.Type)
        }
    }
}

But it falls to panic in a case of pointers. How to fix that?

EDIT: got what I needed:

func printFields(prefix string, t reflect.Type) {
    if t.Kind() != reflect.Struct {
        return
    }
    for i := 0; i < t.NumField(); i++ {
        f := t.Field(i)
        fmt.Printf("%v%v %v
", prefix, f.Name, f.Type)
        if f.Type.Kind() == reflect.Struct {
            fmt.Println(reflect.New(f.Type))
            printFields(fmt.Sprintf("  %v", prefix), f.Type)
        } else if f.Type.Kind() == reflect.Ptr {
            printFields(fmt.Sprintf("  %v", prefix), f.Type.Elem())
        }
    }
}

func printExpandedStruct(s interface{}) {
    printFields("", reflect.ValueOf(s).Type())
}
  • 写回答

1条回答 默认 最新

  • duange051858 2018-10-08 14:11
    关注

    If the field type is a pointer, then use the element type of the pointer. Otherwise, the logic is the same as in the linked question.

    func printFields(prefix string, t reflect.Type) {
        for i := 0; i < t.NumField(); i++ {
            f := t.Field(i)
            fmt.Printf("%v%v %v
    ", prefix, f.Name, f.Type)
            ft := f.Type
            if ft.Kind() == reflect.Ptr {
                ft = ft.Elem()
            }
            if ft.Kind() == reflect.Struct {
                printFields(fmt.Sprintf("  %v", prefix), ft)
            }
        }
    }
    

    Run it on the playground.

    The following code handles the more general case where the fields can be arrays, slices, channels, pointers or combinations of these kinds.

    func printFields(prefix string, t reflect.Type) {
        for i := 0; i < t.NumField(); i++ {
            f := t.Field(i)
            fmt.Printf("%v%v %v
    ", prefix, f.Name, f.Type)
            ft := f.Type
            for ft.Kind() == reflect.Ptr ||
                ft.Kind() == reflect.Slice ||
                ft.Kind() == reflect.Array ||
                ft.Kind() == reflect.Chan {
                ft = ft.Elem()
            }
            if ft.Kind() == reflect.Struct {
                printFields(fmt.Sprintf("  %v", prefix), ft)
            }
        }
    }
    

    Run it on the playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信