How to compare a types using reflect

6,044 views
Skip to first unread message

Sander van Harmelen

unread,
Apr 22, 2014, 10:04:28 AM4/22/14
to golan...@googlegroups.com
Hi,

I’m trying to compare the type of a field of a struct against a custom type to verify that the struct passed has a named field of a certain type.

Without the test it looks something like this (which works fine):


But I would like to have an additional check on line 24 so it reads something like this (this doesn’t work):

if f.IsValid() && f.Type() == CustomType {
fmt.Println(f)
}

But this gives me an error saying:

'type CustomType is not an expression

Anyone knows the solution?

Thanks!

Sander

Ian Lance Taylor

unread,
Apr 22, 2014, 10:10:32 AM4/22/14
to Sander van Harmelen, golan...@googlegroups.com
Use reflect.TypeOf to get something you can compare to f.Type().

Ian

Matthew Kane

unread,
Apr 22, 2014, 10:10:58 AM4/22/14
to Sander van Harmelen, golan...@googlegroups.com
Call reflect.TypeOf() on an instance of the type you are comparing
against. Try this: http://play.golang.org/p/lXXHfRpek8
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
matt kane
twitter: the_real_mkb / nynexrepublic
http://hydrogenproject.com

Sander van Harmelen

unread,
Apr 22, 2014, 10:16:35 AM4/22/14
to Matthew Kane, golan...@googlegroups.com
AhŠ I did already test with reflect.TypeOf, but on the type directly
instead of on an instance of the type. Although it feels it bit weird to
create a variable only for this purpose, it does works fine this way.
Thanks!

opennota

unread,
Apr 22, 2014, 10:23:55 AM4/22/14
to golan...@googlegroups.com, Matthew Kane
On Tuesday, April 22, 2014 9:16:35 PM UTC+7, Sander van Harmelen wrote:
 it feels it bit weird to create a variable only for this purpose

Sander van Harmelen

unread,
Apr 22, 2014, 10:47:50 AM4/22/14
to opennota, golan...@googlegroups.com, Matthew Kane
Cool :) Like this one even better… But I must admit I don’t completely understand what you are doing here… Do I understand it correctly that you type cast a nil value to a pointer of the type to test against? And then the Elem() works to retrieve the underlaying type as it is now a pointer? 

S.


--

opennota

unread,
Apr 22, 2014, 10:49:10 AM4/22/14
to golan...@googlegroups.com, opennota, Matthew Kane
On Tuesday, April 22, 2014 9:47:50 PM UTC+7, Sander van Harmelen wrote:
Do I understand it correctly that you type cast a nil value to a pointer of the type to test against? And then the Elem() works to retrieve the underlaying type as it is now a pointer? 

Exactly.
 

chris dollin

unread,
Apr 22, 2014, 11:15:07 AM4/22/14
to Sander van Harmelen, opennota, golan...@googlegroups.com, Matthew Kane
On 22 April 2014 15:47, Sander van Harmelen <SvanHa...@schubergphilis.com> wrote:
Do I understand it correctly that you type cast a nil value to a pointer of the type to test against?

Nitpick: Go calls that a "conversion" and doesn't call anything a "type cast".
Go's conversions don't have the power, nor the danger, of C's juggling
fiery  razor-blades oh-look-its-a-pointer-to-whateverIlike pointer-type casts.

Chris

(And all C's casts are "type casts" so you can just call them "casts".)

--
Chris "allusive" Dollin

Sander van Harmelen

unread,
Apr 22, 2014, 12:17:01 PM4/22/14
to chris dollin, opennota, golan...@googlegroups.com, Matthew Kane
Check... Did read that some time ago, but seem to have slipped my mind.

So thanks for reminding me!

Sander
Reply all
Reply to author
Forward
0 new messages