-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathreflection-examples.go
68 lines (57 loc) · 1.27 KB
/
reflection-examples.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
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"reflect"
)
type User struct {
name string
age int
}
func (u User) PrintName() {
fmt.Println(u.name)
}
func (u User) PrintAge() {
fmt.Println(u.age)
}
type Aggregator func(int, int) int
var (
user = User{
name: "heli",
age: 24,
}
add Aggregator = func(a, b int) int {
return a + b
}
sub Aggregator = func(a, b int) int {
return a - b
}
)
func inspect(variable interface{}) {
t := reflect.TypeOf(variable)
v := reflect.ValueOf(variable)
if t.Kind() == reflect.Struct {
// print its fields
fmt.Printf("--------- fields %d ----------\n", t.NumField())
for idx := 0; idx < t.NumField(); idx++ {
fieldType := t.Field(idx)
fieldVal := v.Field(idx)
fmt.Printf("\t %v = %v\n", fieldType.Name, fieldVal)
}
// iterate its methods
fmt.Printf("--------- methods %d ----------\n", t.NumMethod())
for idx := 0; idx < t.NumMethod(); idx++ {
methodType := t.Method(idx)
fmt.Printf("\t method_name=%s input_num=%d, output_num=%d\n",
methodType.Name,
methodType.Type.NumIn(), methodType.Type.NumOut())
}
} else if t.Kind() == reflect.Func {
fmt.Printf("this function has %d inputs and %d outputs\n", t.NumIn(), t.NumOut())
}
fmt.Printf("\n\n")
}
func main() {
//inspect(user)
inspect(add)
inspect(sub)
}