dtqjbbr5283 2017-12-05 23:23
浏览 46

golang递归地反映字段类型和值

In golang, I want to recursively reflect through a struct, getting the name of the field, it's type and the value.

The code here helped me reflect golang recurisive reflection

The problem is when I try to extract the value I keep getting panics when I reflect the value on a ptr Value. Is it possible to reflect both the type, and keep passing the value through until I get to the primitives, and at that point print both the field name, type and value?

Here is the code I modified:

    func printType(prefix string, t reflect.Type, v reflect.Value visited map[reflect.Type]bool) {

    // Print the name of this type with opening ( for description.
    fmt.Printf("%s (", t)

    // Traverse elements, adding to description as we go.
elems:
    for {
        switch t.Kind() {
        case reflect.Ptr:
            fmt.Print("ptr to ")
        case reflect.Slice:
            fmt.Print("slice of ")
        case reflect.Array:
            fmt.Printf("array with %d elements of ", t.Len())
        default:
            break elems
        }
        t = t.Elem()
    }

    // Print the kind of the type and the closing ) of the description.
    // In the case of a struct, we print the names of the fields and recurse.
    switch t.Kind() {
    case reflect.Struct:
        fmt.Printf("struct with %d fields)
", t.NumField())
        if visited[t] {
            // Don't blow up on recursive type definition.
            break
        }
        visited[t] = true
        prefix += "    "
        for i := 0; i < t.NumField(); i++ {
            f := t.Field(i)
            // Get value for field
            fieldValue := v.Field(i)

            fmt.Print(prefix, f.Name, " ")
            printType(prefix, f.Type, fieldValue, visited)
        }
    default:
        fmt.Printf("%s) : %s
", t.Kind(), v)
    }
}

When I run this I get a panic when calling fieldValue := v.Field(i) Any thoughts on how to achieve this?

Thanks

  • 写回答

1条回答 默认 最新

  • doufen2769 2017-12-06 00:02
    关注

    Try this:

    func printValue(prefix string, v reflect.Value, visited map[interface{}]bool) {
    
        fmt.Printf("%s: ", v.Type())
    
        // Drill down through pointers and interfaces to get a value we can print.
        for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
            if v.Kind() == reflect.Ptr {
                // Check for recursive data
                if visited[v.Interface()] {
                    fmt.Println("visted")
                    return
                }
                visited[v.Interface()] = true
            }
            v = v.Elem()
        }
    
        switch v.Kind() {
        case reflect.Slice, reflect.Array:
            fmt.Printf("%d elements
    ", v.Len())
            for i := 0; i < v.Len(); i++ {
                fmt.Printf("%s%d: ", prefix, i)
                printValue(prefix+"   ", v.Index(i), visited)
            }
        case reflect.Struct:
            t := v.Type() // use type to get number and names of fields
            fmt.Printf("%d fields
    ", t.NumField())
            for i := 0; i < t.NumField(); i++ {
                fmt.Printf("%s%s: ", prefix, t.Field(i).Name)
                printValue(prefix+"   ", v.Field(i), visited)
            }
        case reflect.Invalid:
            fmt.Printf("nil
    ")
        default:
            fmt.Printf("%v
    ", v.Interface())
        }
    }
    

    Because it's possible to get the type from a value, there's no need to pass types to the print function.

    playground example

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害