package main
import (
"fmt"
"reflect"
)
type Student struct {
Id string
Name string
Age int
Score float64
}
func main() {
student := Student{
Id: "st01",
Name: "Name 1",
Age: 20,
Score: 5.6,
}
tp := reflect.TypeOf(student)
fmt.Println(tp.Name())
fmt.Println(tp.NumField())
for i := 0; i < tp.NumField(); i++ {
fmt.Println(tp.Field(i).Name, ":", tp.Field(i).Type)
fmt.Println("-----------------------")
}
}
Student
4
Id : string
-----------------------
Name : string
-----------------------
Age : int
-----------------------
Score : float64
-----------------------