FAQ
Greetings,

If you open http://play.golang.org/p/nrjINWcjn9 and click "Run" - you'll see

prog.go:17: invalid operation: v += 1 (mismatched types interface {} and int)
  [process exited with non-zero status]


Is there a way to return variable of certain type from Get without creating
another method called GetInt? I guess no but just clarifying to be sure.

What I want is to have a global stash in HTTP daemon where I can keep this
or that stuff.

Cheers
Eugene

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

  • Kornel Maczyński at Aug 16, 2014 at 7:54 pm
    Hi,
    Go is statically typed so the type of v needs to be known at declaration
    (in your case it's interface{}). If you don't want to create methods
    returning different types (like GetInt), you can have a look how json
    unmarshalling works in Go:
    https://golang.org/pkg/encoding/json/#example_Unmarshal

    In a nutshell, your Get method could take as arguments key and v
    interface{}. The value would be put to a place pointed to by a pointer
    you'd pass as v:

    var i int
    stash.Get("key", &i)

    Implementation of Get would use the reflect package


    Regards,
    Kornel

    On Saturday, August 16, 2014 8:07:23 PM UTC+1, Eugene Toropov wrote:

    Greetings,

    If you open http://play.golang.org/p/nrjINWcjn9 and click "Run" - you'll
    see

    prog.go:17: invalid operation: v += 1 (mismatched types interface {} and int)
    [process exited with non-zero status]


    Is there a way to return variable of certain type from Get without
    creating another method called GetInt? I guess no but just clarifying to be
    sure.

    What I want is to have a global stash in HTTP daemon where I can keep this
    or that stuff.

    Cheers
    Eugene
    --
    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.
  • Eugene Toropov at Aug 17, 2014 at 5:34 am
    Hi Kornel,

    Thanks, I'll try it.

    Cheers
    Eugene
    On Saturday, August 16, 2014 11:54:20 PM UTC+4, Kornel Maczyński wrote:

    Hi,
    Go is statically typed so the type of v needs to be known at declaration
    (in your case it's interface{}). If you don't want to create methods
    returning different types (like GetInt), you can have a look how json
    unmarshalling works in Go:
    https://golang.org/pkg/encoding/json/#example_Unmarshal

    In a nutshell, your Get method could take as arguments key and v
    interface{}. The value would be put to a place pointed to by a pointer
    you'd pass as v:

    var i int
    stash.Get("key", &i)

    Implementation of Get would use the reflect package


    Regards,
    Kornel

    On Saturday, August 16, 2014 8:07:23 PM UTC+1, Eugene Toropov wrote:

    Greetings,

    If you open http://play.golang.org/p/nrjINWcjn9 and click "Run" - you'll
    see

    prog.go:17: invalid operation: v += 1 (mismatched types interface {} and int)
    [process exited with non-zero status]


    Is there a way to return variable of certain type from Get without
    creating another method called GetInt? I guess no but just clarifying to be
    sure.

    What I want is to have a global stash in HTTP daemon where I can keep
    this or that stuff.

    Cheers
    Eugene
    --
    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.
  • Rogerjd at Aug 18, 2014 at 2:36 pm
    Just check the type,
    http://play.golang.org/p/_VRXthk8PW
    Roger
    On Sunday, August 17, 2014 1:34:27 AM UTC-4, Eugene Toropov wrote:

    Hi Kornel,

    Thanks, I'll try it.

    Cheers
    Eugene
    On Saturday, August 16, 2014 11:54:20 PM UTC+4, Kornel Maczyński wrote:

    Hi,
    Go is statically typed so the type of v needs to be known at declaration
    (in your case it's interface{}). If you don't want to create methods
    returning different types (like GetInt), you can have a look how json
    unmarshalling works in Go:
    https://golang.org/pkg/encoding/json/#example_Unmarshal

    In a nutshell, your Get method could take as arguments key and v
    interface{}. The value would be put to a place pointed to by a pointer
    you'd pass as v:

    var i int
    stash.Get("key", &i)

    Implementation of Get would use the reflect package


    Regards,
    Kornel

    On Saturday, August 16, 2014 8:07:23 PM UTC+1, Eugene Toropov wrote:

    Greetings,

    If you open http://play.golang.org/p/nrjINWcjn9 and click "Run" -
    you'll see

    prog.go:17: invalid operation: v += 1 (mismatched types interface {} and int)
    [process exited with non-zero status]


    Is there a way to return variable of certain type from Get without
    creating another method called GetInt? I guess no but just clarifying to be
    sure.

    What I want is to have a global stash in HTTP daemon where I can keep
    this or that stuff.

    Cheers
    Eugene
    --
    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
postedAug 16, '14 at 7:07p
activeAug 18, '14 at 2:36p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase