FAQ
Can anyone explain why this code is not working? When decoding/encoding I
cant recover my object which has fields of map[string]interface{}

I have tried to register the map[string]interface{} to make it work using
gob.Register(map[string]interface{}{}).

These functions are in an external package if that helps.

Thanks,

func Encode_gob(logs chan string, input interface{}) (bool, []byte) {
if input == nil { logs<-"TOOLS/GOB/ENCODE: INPUT INTERFACE IS NIL"; return
false, nil }
encoded := new(bytes.Buffer)
gob.Register(map[string]interface{}{})
encCache := gob.NewEncoder(encoded)
encCache.Encode(input)
return true, encoded.Bytes()
}

func Decode_gob(logs chan string, input []byte, data interface{}) bool {
dCache := bytes.NewBuffer(input)
gob.Register(map[string]interface{}{})
decCache := gob.NewDecoder(dCache)
e := decCache.Decode(data)
if e != nil || data == nil { logs<-"TOOLS/GOB/DECODE: "+e.Error(); return
false }
return true
}

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

  • Alex Howard at Oct 11, 2014 at 10:08 pm

    I found this of rob moaning that he shouldn't have included support for
    interface{} in Gob but no real answers:
    https://groups.google.com/forum/#!topic/golang-dev/_t4pqoeuflE

    --
    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.
  • Rob Pike at Oct 11, 2014 at 11:00 pm
    You don't say anything about the problem beyond "it doesn't work".
    Without a full executable example others can try to see the issue, you
    can't expect much help.

    Also, again and strongly, check your error returns.

    Since map[string]interface{} is not a named type, there is no need to
    register it.

    -rob

    On Sat, Oct 11, 2014 at 3:08 PM, Alex Howard wrote:
    I found this of rob moaning that he shouldn't have included support for
    interface{} in Gob but no real answers:

    https://groups.google.com/forum/#!topic/golang-dev/_t4pqoeuflE

    --
    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.
  • Alex Howard at Oct 11, 2014 at 11:31 pm
    Gob returns EOF when decoding a struct with a field of
    map[string]interface{}

    The only examples I found online *were* registering map[string]interface{}
    and []interface{} as in link below.

    https://socketloop.com/references/golang-encoding-gob-register-function-examples

    I'm working with a large program and shouldn't need to write a whole new
    not-working example program for someone to explain how to encode/decode
    map[string]interface{} with gob.

    Thanks anyway,

    Alex

    On Sunday, 12 October 2014 01:00:41 UTC+2, Rob 'Commander' Pike wrote:

    You don't say anything about the problem beyond "it doesn't work".
    Without a full executable example others can try to see the issue, you
    can't expect much help.

    Also, again and strongly, check your error returns.

    Since map[string]interface{} is not a named type, there is no need to
    register it.

    -rob


    On Sat, Oct 11, 2014 at 3:08 PM, Alex Howard <3du...@gmail.com
    <javascript:>> wrote:
    I found this of rob moaning that he shouldn't have included support for
    interface{} in Gob but no real answers:

    https://groups.google.com/forum/#!topic/golang-dev/_t4pqoeuflE

    --
    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...@googlegroups.com <javascript:>.
    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.
  • Jesper Louis Andersen at Oct 11, 2014 at 11:51 pm

    On Sun, Oct 12, 2014 at 1:31 AM, Alex Howard wrote:

    I'm working with a large program and shouldn't need to write a whole new
    not-working example program for someone to explain how to encode/decode
    map[string]interface{} with gob.

    Hi!

    I would encourage you to do so anyway and put it on the Go Playground.
    Besides being useful to other people, it forms a good basis for debugging
    in a large program, because it "slices" the problem in two. If the toy
    example works, the program as a whole must be at fault, whereas if the
    small example fails to work, there is perhaps a problem further down the
    library chain in the standard library.

    In addition, code is much better at clearing up misconceptions. Especially
    if it documents the expected output and the obtained output. You might
    simply be using the standard library wrong in which case we can explain
    that. And perhaps improve the documentation if needed.


    --
    J.

    --
    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.
  • Rob Pike at Oct 12, 2014 at 4:33 am
    I'm sorry, yes, the type must be registered. I was, as is often the
    case, confused by the ways people misuse empty interfaces.

    The other point still stands: Without a complete working example
    there's little help to offer.

    -rob

    --
    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.
  • Rob Pike at Oct 12, 2014 at 4:36 am
    ... And when I say the type must be registered, I mean you must
    register the type you will transmity as the empty interface. You do
    not need to register map[string]interface{} unless it, too, is going
    to be transmitted.

    Read the doc for Register carefully:

    // Register records a type, identified by a value for that type, under its
    // internal type name. That name will identify the concrete type of a value
    // sent or received as an interface variable. Only types that will be
    // transferred as implementations of interface values need to be registered.
    // Expecting to be used only during initialization, it panics if the mapping
    // between types and names is not a bijection.

    On Sat, Oct 11, 2014 at 9:33 PM, Rob Pike wrote:
    I'm sorry, yes, the type must be registered. I was, as is often the
    case, confused by the ways people misuse empty interfaces.

    The other point still stands: Without a complete working example
    there's little help to offer.

    -rob
    --
    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.
  • Jakob Borg at Oct 12, 2014 at 5:19 am
    Seems working enough; at least when lightly modified and put in a context?

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

    2014-10-11 22:57 GMT+02:00 Alex Howard <3dukltd@gmail.com>:
    Can anyone explain why this code is not working? When decoding/encoding I
    cant recover my object which has fields of map[string]interface{}

    I have tried to register the map[string]interface{} to make it work using
    gob.Register(map[string]interface{}{}).

    These functions are in an external package if that helps.

    Thanks,

    func Encode_gob(logs chan string, input interface{}) (bool, []byte) {
    if input == nil { logs<-"TOOLS/GOB/ENCODE: INPUT INTERFACE IS NIL"; return
    false, nil }
    encoded := new(bytes.Buffer)
    gob.Register(map[string]interface{}{})
    encCache := gob.NewEncoder(encoded)
    encCache.Encode(input)
    return true, encoded.Bytes()
    }

    func Decode_gob(logs chan string, input []byte, data interface{}) bool {
    dCache := bytes.NewBuffer(input)
    gob.Register(map[string]interface{}{})
    decCache := gob.NewDecoder(dCache)
    e := decCache.Decode(data)
    if e != nil || data == nil { logs<-"TOOLS/GOB/DECODE: "+e.Error(); return
    false }
    return true
    }

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 11, '14 at 8:57p
activeOct 12, '14 at 5:19a
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase