type Person struct { Name string Age int } func main() { t := reflect.TypeOf(Person{}) fmt.Println(t.Name()) // "Person" fmt.Println(t.Kind()) // "struct" }
type Person struct { Name string Age int } func main() { t := reflect.TypeOf(Person{}) for i := 0; i < t.NumField(); i++ { field := t.Field(i) fmt.Println(field.Name, field.Type) } }In this example, we define a struct type called Person and then use the reflect package to retrieve its type. We use the NumField() method to get the number of fields in the struct, and then loop through them using the Field() method. We print out the name and type of each field. Package library: "reflect"