My question is related to this question here:
golang - Elem Vs Indirect in the reflect package
Basically it claims that the expression below is true if someX
is a reflect.Value
that contains a pointer
reflect.Indirect(reflect.ValueOf(someX)) === reflect.ValueOf(someX).Elem()
If that is the case, then why does my code below crash on the last line?
package main
import (
"reflect"
"log"
)
type Person struct {
Name string
}
func main() {
newitem := reflect.New(reflect.ValueOf(Person{}).Type())
log.Println(reflect.TypeOf(newitem)) // shows reflect.Value
log.Println(newitem.Type().Kind()) // shows it is a ptr
log.Println(reflect.Indirect(reflect.ValueOf(newitem))) // this line does not cause panic
log.Println(reflect.ValueOf(newitem).Elem()) // this line causes panic
}
I've been having a hard time understanding the reflect package in Go, and it is likely I've misunderstood some fundamental aspects of the Go language as denoted in stack overflow questions I've been asking the past week.