FAQ
Basically i'm checking whether the second input argument is of type
Context (which is a struct).

I did consider using reflect.AssignableTo(), however that takes a Type as
input, so in order to get the type i have to actually create an instance of
Context to pass to reflect.TypeOf(). So that seems like a waste of
resources, so i ended up with this:

// t = reflect.Type
//TODO is this _really_ a good check? naw..
if name := t.In(1).Name(); name != "Context" {
panic(fmt.Sprintf("Expected 'Context' as input argument, not: %s", name))
}

The reason why i don't like this check is that "Context" is hardcoded in
there, so that would break if the struct Context ever changed name but not
at compile time, which is preferred.

So any opinions on how this can be improved?

Much appreciated
Sincerely Kim

--

Search Discussions

  • Minux at Jan 18, 2013 at 6:33 pm

    On Sat, Jan 19, 2013 at 2:28 AM, Kim Eik wrote:
    Basically i'm checking whether the second input argument is of type Context
    (which is a struct).

    I did consider using reflect.AssignableTo(), however that takes a Type as
    input, so in order to get the type i have to actually create an instance of
    Context to pass to reflect.TypeOf(). So that seems like a waste of
    resources, so i ended up with this:

    // t = reflect.Type
    //TODO is this _really_ a good check? naw..
    if name := t.In(1).Name(); name != "Context" {
    panic(fmt.Sprintf("Expected 'Context' as input argument, not: %s", name))
    }

    The reason why i don't like this check is that "Context" is hardcoded in
    there, so that would break if the struct Context ever changed name but not
    at compile time, which is preferred.

    So any opinions on how this can be improved?
    if only Context-typed things is acceptable, why not just use Context as the type
    for that parameter?
    I guess there are other issues involved, but you didn't mention.

    --
  • Rémy Oudompheng at Jan 18, 2013 at 6:33 pm

    On 2013/1/18 Kim Eik wrote:
    Basically i'm checking whether the second input argument is of type Context
    (which is a struct).

    I did consider using reflect.AssignableTo(), however that takes a Type as
    input, so in order to get the type i have to actually create an instance of
    Context to pass to reflect.TypeOf(). So that seems like a waste of
    resources
    The common pattern is to store the reflect.Type corresponding to your
    type in a global variable, so you only do it once:

    var typeofContext = reflect.TypeOf(Context{})

    --
  • Rémy Oudompheng at Jan 18, 2013 at 6:35 pm

    On 2013/1/18 Kim Eik wrote:
    Basically i'm checking whether the second input argument is of type Context
    (which is a struct).
    The argument of what? You should show your code.
    You may be using reflection where not necessary: maybe you actually
    want a type assertion in your code.

    Rémy.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 18, '13 at 6:28p
activeJan 18, '13 at 6:35p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase