3

我想递归地反映结构类型和值,但它失败了。我不知道如何递归地传递子结构。

它错误如下。

panic: reflect: NumField of non-struct type

goroutine 1 [running]:
reflect.(*rtype).NumField(0xc0b20, 0xc82000a360)
    /usr/local/go/src/reflect/type.go:660 +0x7b

我有两个结构PersonName

type Person struct {
    Fullname NameType
    Sex      string
}

type Name struct {
    Firstname string
    Lastname  string
}

Person在 main 中定义,并用递归函数显示结构。

person := Person{
    Name{"James", "Bound"},
    "Male",
}

display(&person)

display函数递归显示结构。

func display(s interface{}) {
    reflectType := reflect.TypeOf(s).Elem()
    reflectValue := reflect.ValueOf(s).Elem()

    for i := 0; i < reflectType.NumField(); i++ {
        typeName := reflectType.Field(i).Name

        valueType := reflectValue.Field(i).Type()
        valueValue := reflectValue.Field(i).Interface()

        switch reflectValue.Field(i).Kind() {
        case reflect.String:
            fmt.Printf("%s : %s(%s)\n", typeName, valueValue, valueType)
        case reflect.Int32:
            fmt.Printf("%s : %i(%s)\n", typeName, valueValue, valueType)
        case reflect.Struct:
            fmt.Printf("%s : it is %s\n", typeName, valueType)
            display(&valueValue)
        }

    }
}
4

1 回答 1

6

在您的display函数内部,您声明valueValue为:

valueValue := reflectValue.Field(i).Interface()

valueValue类型也是如此interface{}。在 for 循环中,您可以递归调用display

display(&valueValue)

所以它是用 type 的参数调用的*interface{}。在递归调用内部,reflectType将表示interface{}而不是碰巧存储在值中的类型。由于NumField只能在reflect.Type的表示结构上调用,因此您会感到恐慌。

如果你想用指向结构的指针调用 display ,你可以这样做:

v := valueValue := reflectValue.Field(i).Addr()
display(v.Interface())
于 2015-12-28T13:12:38.660 回答