直接上代码,初略的写了一下,具体使用按照自身逻辑改改。

package main

import (
    "fmt"
    "reflect"
)

type Student struct {

    Class string

    School string

    Love string

    StudentBase
}

type StudentBase struct {
    Name string
    Age  int
}

func main() {

    var student Student

    student.Class = "三年一班"
    student.School = "附属小学"
    student.Love = "阅读、游戏"
    student.Name = "李华"
    student.Age = 8

    t := reflect.TypeOf(student)

    if t.Kind() == reflect.Ptr {
        t = t.Elem()
    }

    for i := 0; i < t.NumField(); i++ {
        fieldName := t.Field(i).Name

        //struct 字段
        if fieldName == "StudentBase" {

            fmt.Println("Struct Property StudentBase")
            child := t.Field(i).Type
            if child.Kind() == reflect.Struct {

                for j := 0; j < child.NumField(); j++ {

                    jFieldName := child.Field(j).Name
                    jFieldValue := reflect.ValueOf(student.StudentBase).FieldByName(jFieldName)

                    fmt.Println("name:", jFieldName, "value:", jFieldValue)
                }
                continue
            }
        }
        fieldValue := reflect.ValueOf(student).FieldByName(fieldName)
        fmt.Println("name:", fieldName, "value:", fieldValue)
    }

}