Closed mybigman closed 1 month ago
You can only access nested structure fields, not slices.
data.Kids is a slice of Kids
. Which kid's name do you want to access the first item in the kids slice? The last?
Here's the code that grabs the field value from your struct:
func fieldValue(value reflect.Value, field string) (reflect.Value, error) {
fields := strings.Split(field, ".")
current := value
for i := range fields {
if current.Kind() == reflect.Ptr {
current = current.Elem().FieldByName(fields[i])
} else {
current = current.FieldByName(fields[i])
}
if !current.IsValid() {
return reflect.Value{}, fmt.Errorf("The field %s does not exist in the type %s", field, value)
}
}
return current, nil
}
Not sure whats going on here?