Files
  • main.go
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
package main

import (
	"fmt"
	"reflect"
)

type MyStruct struct {
	Hello string
}

func main() {
	mStruct := MyStruct{}
	tStruct := struct{
		Hello string
	}{}
	
	av := reflect.ValueOf(mStruct)
	bv := reflect.ValueOf(tStruct)
	cv := reflect.Indirect(reflect.ValueOf(mStruct))
	dv := reflect.Indirect(reflect.ValueOf(tStruct))
	ev := reflect.Indirect(reflect.ValueOf(&mStruct))
	fv := reflect.Indirect(reflect.ValueOf(&tStruct))
	gv := reflect.ValueOf(&mStruct).Elem()
	hv := reflect.ValueOf(&tStruct).Elem()

	for k, v := range map[string]reflect.Value{
		"av": av,
		"bv": bv,
		"cv": cv,
		"dv": dv,
		"ev": ev,
		"fv": fv,
		"gv": gv,
		"hv": hv,
	} {
		f := v.FieldByName("Hello")
		fmt.Printf("%q \t IsValid: %t \t CanSet: %t\n", k, 
			f.IsValid(), f.CanSet())
	}
}
go version go1.9.4 linux/amd64