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)\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)
}
}
}