FAQ
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):

http://play.golang.org/p/zBOup309yx

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

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

  • Ian Lance Taylor at Apr 22, 2014 at 2:10 pm

    On Tue, Apr 22, 2014 at 7:04 AM, Sander van Harmelen wrote:

    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):

    http://play.golang.org/p/zBOup309yx

    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?
    Use reflect.TypeOf to get something you can compare to f.Type().

    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.
  • Matthew Kane at Apr 22, 2014 at 2:18 pm
    Call reflect.TypeOf() on an instance of the type you are comparing
    against. Try this: http://play.golang.org/p/lXXHfRpek8


    On Tue, Apr 22, 2014 at 10:04 AM, Sander van Harmelen
    wrote:
    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):

    http://play.golang.org/p/zBOup309yx

    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

    --
    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.


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

    --
    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.
  • Sander van Harmelen at Apr 22, 2014 at 2:16 pm
    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!
    On 22/04/2014 16:10:58, "Matthew Kane" wrote:

    Call reflect.TypeOf() on an instance of the type you are comparing
    against. Try this: http://play.golang.org/p/lXXHfRpek8


    On Tue, Apr 22, 2014 at 10:04 AM, Sander van Harmelen
    wrote:
    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):

    http://play.golang.org/p/zBOup309yx

    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

    --
    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.


    --
    matt kane
    twitter: the_real_mkb / nynexrepublic
    http://hydrogenproject.com
    --
    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.
  • Opennota at Apr 22, 2014 at 2:23 pm

    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
    There's another way: http://play.golang.org/p/t1GIrUujyF

    --
    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.
  • Sander van Harmelen at Apr 22, 2014 at 2:47 pm
    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.


    From: opennota <opennota@gmail.com
    Date: Tuesday 22 April 2014 16:23:00
    To: "golang-nuts@googlegroups.com " <golang-nuts@googlegroups.com
    Cc: Matthew Kane <matt@hydrogenproject.com
    Subject: Re: [go-nuts] How to compare a types using reflect

    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

    There's another way: http://play.golang.org/p/t1GIrUujyF

    --
    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.

    --
    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.
  • Opennota at Apr 22, 2014 at 2:49 pm

    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.


    --
    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.
  • Chris dollin at Apr 22, 2014 at 3:15 pm

    On 22 April 2014 15:47, 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?
    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

    --
    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.
  • Sander van Harmelen at Apr 22, 2014 at 4:17 pm
    Check... Did read that some time ago, but seem to have slipped my mind.

    So thanks for reminding me!

    Sander

    On 22 apr. 2014, at 17:15, "chris dollin" wrote:

    On 22 April 2014 15:47, 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?

    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

    --
    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
postedApr 22, '14 at 2:04p
activeApr 22, '14 at 4:17p
posts9
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase