FAQ
Hello,

I ran into a problem and I could use a little help, I want to be able to
distinguish between a *method that belongs to a struct* and a *func* using
the reflect package.

Here's the problem: http://play.golang.org/p/zpOwzq_xbg

First, I assign both functions to variables a and b.

Then, I would like to know which of the variables (a or b) is the one that
belongs to an struct (any struct) using only the reflect package. I don't
want to compare types against the trivialStruct I just need a way to
distinguish which one of them is part of a struct and which one is not.

Can you help me a bit? I know that there's a reflect.Struct type but I
don't know how can it be applied here.

Thanks,

José Carlos.

--

Search Discussions

  • Steven Blenkinsop at Jan 20, 2013 at 12:27 am
    I'm not sure why you would want to make this distinction, but this code
    seems to work: http://play.golang.org/p/GD_ZoSpnRe
    It uses the runtime package to determine the name of the function. I'm not
    sure if this violates any constraints, since you said using only the
    reflect package (I assumed you just meant without having to statically know
    the type). You could, for example, just iterate over all the methods and
    compare them, I just figured this would be a workable short cut.

    On Sat, Jan 19, 2013 at 4:48 PM, José Carlos Nieto wrote:

    Hello,

    I ran into a problem and I could use a little help, I want to be able to
    distinguish between a *method that belongs to a struct* and a *func*using the reflect package.

    Here's the problem: http://play.golang.org/p/zpOwzq_xbg

    First, I assign both functions to variables a and b.

    Then, I would like to know which of the variables (a or b) is the one that
    belongs to an struct (any struct) using only the reflect package. I don't
    want to compare types against the trivialStruct I just need a way to
    distinguish which one of them is part of a struct and which one is not.

    Can you help me a bit? I know that there's a reflect.Struct type but I
    don't know how can it be applied here.

    Thanks,

    José Carlos.

    --

    --
  • José Carlos Nieto at Jan 20, 2013 at 12:31 pm
    Hi Steve,

    Your assumption was right and that's a clever workaround!

    Thank you.
    On Saturday, January 19, 2013 6:27:02 PM UTC-6, Steven Blenkinsop wrote:

    I'm not sure why you would want to make this distinction, but this code
    seems to work: http://play.golang.org/p/GD_ZoSpnRe
    It uses the runtime package to determine the name of the function. I'm not
    sure if this violates any constraints, since you said using only the
    reflect package (I assumed you just meant without having to statically know
    the type). You could, for example, just iterate over all the methods and
    compare them, I just figured this would be a workable short cut.


    On Sat, Jan 19, 2013 at 4:48 PM, José Carlos Nieto <xi...@menteslibres.org<javascript:>
    wrote:
    Hello,

    I ran into a problem and I could use a little help, I want to be able to
    distinguish between a *method that belongs to a struct* and a *func*using the reflect package.

    Here's the problem: http://play.golang.org/p/zpOwzq_xbg

    First, I assign both functions to variables a and b.

    Then, I would like to know which of the variables (a or b) is the one
    that belongs to an struct (any struct) using only the reflect package. I
    don't want to compare types against the trivialStruct I just need a way to
    distinguish which one of them is part of a struct and which one is not.

    Can you help me a bit? I know that there's a reflect.Struct type but I
    don't know how can it be applied here.

    Thanks,

    José Carlos.

    --

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 19, '13 at 9:54p
activeJan 20, '13 at 12:31p
posts3
users2
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase