-
Notifications
You must be signed in to change notification settings - Fork 29
/
reflection.go
46 lines (37 loc) · 997 Bytes
/
reflection.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
package main
import (
"fmt"
"reflect"
)
type Secret struct {
Username string
Password string
}
type Record struct {
Field1 string
Field2 float64
Field3 Secret
}
func main() {
A := Record{"String value", -12.123, Secret{"Mihalis", "Tsoukalos"}}
r := reflect.ValueOf(A)
fmt.Println("String value:", r.String())
iType := r.Type()
fmt.Printf("i Type: %s\n", iType)
fmt.Printf("The %d fields of %s are\n", r.NumField(), iType)
for i := 0; i < r.NumField(); i++ {
fmt.Printf("\t%s ", iType.Field(i).Name)
fmt.Printf("\twith type: %s ", r.Field(i).Type())
fmt.Printf("\tand value _%v_\n", r.Field(i).Interface())
// Check whether there are other structures embedded in Record
k := reflect.TypeOf(r.Field(i).Interface()).Kind()
// Need to convert it to string in order to compare it
if k.String() == "struct" {
fmt.Println(r.Field(i).Type())
}
// Same as before but using the internal value
if k == reflect.Struct {
fmt.Println(r.Field(i).Type())
}
}
}