drgzmmy6379 2015-12-28 11:54
浏览 79

如何在Golang中反映结构递归

I want to reflect the struct type and value recursively, but it fails. I don't know how to pass the sub struct recursively.

It error the following.

panic: reflect: NumField of non-struct type

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

I have two struct Person and Name

type Person struct {
    Fullname NameType
    Sex      string
}

type Name struct {
    Firstname string
    Lastname  string
}

I define the Person in main, and display the struct with recursive function.

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

display(&person)

The display function recursive display the struct.

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)
", typeName, valueValue, valueType)
        case reflect.Int32:
            fmt.Printf("%s : %i(%s)
", typeName, valueValue, valueType)
        case reflect.Struct:
            fmt.Printf("%s : it is %s
", typeName, valueType)
            display(&valueValue)
        }

    }
}
  • 写回答

1条回答 默认 最新

  • doufang8965 2015-12-28 13:12
    关注

    Inside your display function, you declare valueValue as:

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

    So valueValue is of type interface{}. Inside the for loop, you have a recursive call to display:

    display(&valueValue)
    

    So it is being called with an argument of type *interface{}. Inside the recursive call, reflectType will represent interface{} rather than the type that happens to be stored within the value. Since NumField can only be called on reflect.Type's representing structs, you get a panic.

    If you want to call display with a pointer to the struct instead, you could do so with something like this:

    v := valueValue := reflectValue.Field(i).Addr()
    display(v.Interface())
    
    评论

报告相同问题?