func NewA() A { return A{10} } ...(snip)... func main() { a := sub.NewA() v := reflect.ValueOf(a).FieldByName("a") v.SetInt(100) fmt.Println(a) } panic: reflect: reflect.Value.SetInt using value obtained using unexported field
◦ The Laws of Reflection (The Go Blog) ▪ https://blog.golang.org/laws-of-reflection ◦ Settabilityの説明だけどAddressabilityも似た感じ a := sub.NewA() v := reflect.ValueOf(a).FieldByName("a") A a A a 変数 a FieldByName の戻り値 ValueOf の引数 v ValueOf の戻り値 コピー unaddressable addressable
◦ コピーではないので Addressable • 戻り値 v もAddressable A a 変数 a FieldByName の戻り値 ValueOf の引数 v ValueOf の戻り値 コピー &a Elem の戻り値 unaddressable addressable a := sub.NewA() v := reflect.ValueOf(&a).Elem().FieldByName("a")