FAQ
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!

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Andy Balholm at Feb 4, 2016 at 10:31 pm
    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nuri Kevenoglu at Feb 4, 2016 at 10:39 pm
    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Lance Taylor at Feb 4, 2016 at 10:41 pm

    On Thu, Feb 4, 2016 at 2:39 PM, Nuri Kevenoglu 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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nuri Kevenoglu at Feb 4, 2016 at 11:00 pm
    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...

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jesse McNelis at Feb 5, 2016 at 12:16 am

    On Fri, Feb 5, 2016 at 10:00 AM, Nuri Kevenoglu wrote:
    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.
    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 4, '16 at 10:27p
activeFeb 5, '16 at 12:16a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase