package main
import (
"fmt"
"reflect"
)
type Student struct {
Id string
Name string
Age int
Score float64
}
func (this Student) ToString() string {
return fmt.Sprintf("id: %s\nName: %s\nAge: %d\nScore: %0.1f", this.Id, this.Name, this.Age, this.Score)
}
func (this Student) Rank() string {
if this.Score >= 8 {
return "A"
} else if this.Score >= 7 {
return "B"
} else if this.Score >= 6 {
return "C"
} else if this.Score >= 5 {
return "D"
} else {
return "E"
}
}
func main() {
student := Student{
Id: "st01",
Name: "Name 1",
Age: 20,
Score: 5.6,
}
tp := reflect.TypeOf(student)
fmt.Println(tp.Name())
fmt.Println("Methods: ", tp.NumMethod())
for i := 0; i < tp.NumMethod(); i++ {
fmt.Println(tp.Method(i).Name, ":", tp.Method(i).Type)
fmt.Println("-----------------------")
}
}
Student
Methods: 2
Rank : func(main.Student) string
-----------------------
ToString : func(main.Student) string
-----------------------