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.

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大