v.Elem() Vs Indirect(v) when passing in result of a reflect.New(Type)
Asked Answered
I

1

2

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.

Iroquoian answered 1/9, 2018 at 22:3 Comment(0)
C
4

Let's break down the following line:

log.Println(reflect.ValueOf(newitem).Elem())

The value newItem is a reflect.Value. The expression reflect.ValueOf(newItem) returns a reflect.Value containing a reflect.Value. Because the contained value is not a pointer or interface, the call to Elem() panics.

The following line does not panic because reflect.Indirect returns its argument if the argument is not a pointer type.

 log.Println(reflect.Indirect(reflect.ValueOf(newitem))) 

The problem is that the application is wrapping reflect.Values with reflect.Values. Use the reflect.Value directly as in the following code:

log.Println(reflect.Indirect(newitem))
log.Println(newitem.Elem())

Run it on the Playground!

Coypu answered 1/9, 2018 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.