-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (44 loc) · 924 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"reflect"
)
type Student struct{
Name string
Age int
Score float32
}
func (s Student) Set(name string, age int, score float32){
s.Name = name
s.Age = age
s.Score = score
}
func (s Student) Print(){
fmt.Println("----start----")
fmt.Println(s)
fmt.Println("----end----")
}
func TestStruct(stu interface{}) {
val := reflect.ValueOf(stu)
kd := val.Kind()
if kd != reflect.Struct{
fmt.Println("should be struct type")
return
}
fnum :=val.NumField()
fmt.Printf("struct has %d fileds\n", fnum)
for i:=0; i< fnum; i++{
fmt.Printf("%d %v\n", i, val.Field(i))
}
numOfMethod := val.NumMethod()
fmt.Printf("struct has %d methords.", numOfMethod)
// var params []reflect.Value
// val.Method(0).Call(params)
val.Method(0).Call(nil)
fmt.Printf("%v \n", val.Method(1))
//val.Method(1).Call(params)
}
func main() {
a := Student{"Aaron", 18,95.5}
TestStruct(&a)
}