Need Help. Stumped on how to get reflection Type of struct given a pointer to a struct

2,029 views
Skip to first unread message

Ugorji Nwoke

unread,
Oct 5, 2011, 3:26:31 PM10/5/11
to golan...@googlegroups.com
I have the following:

    var s interface{} = new(MyStruct)
    ... 
    rt := reflect.TypeOf(s)
    ...
    sf, err := rt.FieldByName("abc")

At Runtime, I get
   panic: reflect: FieldByName of non-struct type

On further investigation, I noticed that my reflection type (rt above) is a Type for a pointer to MyStruct (ie *MyStruct), not MyStruct. Consequently, the code panics because I can't get the field name given a type of a pointer.

I've not been able to figure out how to actually get from a reflect.Type for *MyStruct to a reflect.Type for MyStruct. Please help.

------------------------------------------
PS in case this is pertinent, I used the following code to figure the above out. Please let me know if I'm misinterpreting it somehow.
        fmt.Fprintf(os.Stdout, " #### Reflect Type: Of: ((%+v)) is: ((%+v)) with kind: ((%v))\n", s, rt, rt.Kind())
which printed out
       #### Reflect Type: Of: ((&{ ... })) is: ((*MyStruct)) with kind: ((ptr))

Jesse McNelis

unread,
Oct 5, 2011, 3:32:13 PM10/5/11
to golan...@googlegroups.com, Ugorji Nwoke
On 06/10/11 06:26, Ugorji Nwoke wrote:
> I have the following:
>
> var s interface{} = new(MyStruct)
> ...
> rt := reflect.TypeOf(s)
> ...
> sf, err := rt.FieldByName("abc")
>
> At Runtime, I get
> panic: reflect: FieldByName of non-struct type
>
> On further investigation, I noticed that my reflection type (rt above)
> is a Type for a pointer to MyStruct (ie *MyStruct), not MyStruct.
> Consequently, the code panics because I can't get the field name given a
> type of a pointer.
>
> I've not been able to figure out how to actually get from a reflect.Type
> for *MyStruct to a reflect.Type for MyStruct. Please help.

You can just do:

reflect.TypeOf(MyStruct{})

which will give you a reflect.Type for MyStruct

- jessta

Ugorji Nwoke

unread,
Oct 5, 2011, 3:34:37 PM10/5/11
to golan...@googlegroups.com, Ugorji Nwoke, jes...@jessta.id.au
I know. I summarized the snippet so it's focussed on the problem. 

I'm getting the pointer passed down from somewhere else into a function that takes an interface{} argument. 

In this case, I actually just have a pointer but want to get access to a field in the type. 


Tarmigan

unread,
Oct 5, 2011, 3:37:27 PM10/5/11
to golan...@googlegroups.com, Ugorji Nwoke, jes...@jessta.id.au
Have you tried .Elem() ?

If you haven't read it already, you should read the excellent blog
post on Reflection:
http://blog.golang.org/2011/09/laws-of-reflection.html
and especially pay attention to the "Structs" section, which goes
through a complete example and might be useful for you.

Thanks,
Tarmigan

peterGo

unread,
Oct 5, 2011, 3:48:07 PM10/5/11
to golang-nuts
Ugorji,

Are you trying to do something like this?

type MyStruct struct{ abc int }
var s interface{} = new(MyStruct)
rt := reflect.TypeOf(s)
sf, err := rt.Elem().FieldByName("abc")

Peter

Ugorji Nwoke

unread,
Oct 5, 2011, 3:50:05 PM10/5/11
to golan...@googlegroups.com, Ugorji Nwoke, jes...@jessta.id.au
Ah. Thank you, Tarmigan.

I had read the docs (and the blog post), but I didn't think that .Elem() applied to reflect.Type unless it was for a container (Slice or Map or Array). After seeing your response, I looked at the Elem() documentation again and clear as day, it can be used. I tried it and it works.

Thanks much.

Reply all
Reply to author
Forward
0 new messages