noob: type assertion vs reflection

1,721 views
Skip to first unread message

Nuri Kevenoglu

unread,
Feb 4, 2016, 5:28:02 PM2/4/16
to golang-nuts
Hi guys,

I'm sure these questions would've been asked before but I couldn't see a way to search though the topics.

1. Is type assertion a type of reflection?

2. How does type assertion compare to reflection in performance? 

3. Is it recommended to avoid type assertion if possible, like reflection?

I have read the the laws of reflection, but as far as I can see, these questions are not answered there.

Thank you!

Andy Balholm

unread,
Feb 4, 2016, 5:31:43 PM2/4/16
to Nuri Kevenoglu, golang-nuts
Type assertion is much simpler than reflection, and takes much less time. It’s basically just comparing the type pointer in an interface value to another type pointer that represents the type you are asserting to, and then copying the underlying value out of the interface. So don’t worry about its performance.

Nuri Kevenoglu

unread,
Feb 4, 2016, 5:39:30 PM2/4/16
to golang-nuts, keve...@gmail.com
Great, thank you for the fast response!

Is there a way to check whether a type contains a property by name and type without reflection e.g. contains property "id" (int64) or is reflection the only way? 

What about checking for the existence of an embedded type?

Thanks.

Ian Lance Taylor

unread,
Feb 4, 2016, 5:41:40 PM2/4/16
to Nuri Kevenoglu, golang-nuts
On Thu, Feb 4, 2016 at 2:39 PM, Nuri Kevenoglu <keve...@gmail.com> wrote:
>
> Is there a way to check whether a type contains a property by name and type
> without reflection e.g. contains property "id" (int64) or is reflection the
> only way?
>
> What about checking for the existence of an embedded type?

Checking for a struct field, embedded or not, can only be done using reflection.

You can use a (much faster) type assertion to check for a method.

Ian

Nuri Kevenoglu

unread,
Feb 4, 2016, 6:00:11 PM2/4/16
to golang-nuts, keve...@gmail.com
Thank you!

Last question. Is it common/feasible to create an interface method called something like "GetTypeInfo()" that returns information about the type (e.g. property list and types)? I'm thinking that way I can use type assertion and get type information without reflection...

Jesse McNelis

unread,
Feb 4, 2016, 7:17:12 PM2/4/16
to Nuri Kevenoglu, golang-nuts
You certainly can, it really depends on what you're using reflection for.
You wouldn't be able to use that information to access fields or call
functions etc. and you'll have to keep it up to date manually.

Maxime FRYSOU

unread,
Jul 3, 2022, 4:37:50 PM7/3/22
to golang-nuts
Hi Ian,

I stumble upon this topic that is very interesting but I'm not 100% sure to fully understand what you meant when you said:
"Checking for a struct field, embedded or not, can only be done using reflection.
You can use a (much faster) type assertion to check for a method."

I know it's been a while, but could you provide a piece of code to illustrate a bit just for clarity?

Thank you so much

Ian Lance Taylor

unread,
Jul 3, 2022, 5:00:46 PM7/3/22
to Maxime FRYSOU, golang-nuts
On Sun, Jul 3, 2022 at 1:37 PM Maxime FRYSOU <maxpr...@gmail.com> wrote:
>
> I stumble upon this topic that is very interesting but I'm not 100% sure to fully understand what you meant when you said:
> "Checking for a struct field, embedded or not, can only be done using reflection.
> You can use a (much faster) type assertion to check for a method."
>
> I know it's been a while, but could you provide a piece of code to illustrate a bit just for clarity?

For example, you can test whether a type has a method "String()
string" by writing

_, hasStringMethod := v.(interface { String() string })

Ian



> On Thursday, 4 February 2016 at 23:41:40 UTC+1 Ian Lance Taylor wrote:
>>
>> On Thu, Feb 4, 2016 at 2:39 PM, Nuri Kevenoglu <keve...@gmail.com> wrote:
>> >
>> > Is there a way to check whether a type contains a property by name and type
>> > without reflection e.g. contains property "id" (int64) or is reflection the
>> > only way?
>> >
>> > What about checking for the existence of an embedded type?
>>
>> Checking for a struct field, embedded or not, can only be done using reflection.
>>
>> You can use a (much faster) type assertion to check for a method.
>>
>> Ian
>
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2f93883c-d257-4732-beec-adabf2a77bd5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages