FAQ
This works: http://play.golang.org/p/nUENUMDM7x

time.Time field is called T

output is
"Pythagoras"
"Treehouse"

So far so good.

This doesn't seem to work: http://play.golang.org/p/tooXqJ5Eio

time.Time field is anonymous

output is
""
""

That's odd.

This panics with a nill dereference: http://play.golang.org/p/kGoY9QA6fG

*time.Time field is anonymous (note: pointer to time.Time)

output is
panic: runtime error: invalid memory address or nil pointer dereference
[recovered]
[...]

Ouch.

Am I doing something wrong?

Thanks!

-- Larry

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

  • Thebrokentoaster at Jan 6, 2016 at 7:48 am
    This seems to be a bug (or two) with the gob package.

        1. It seems that gob does encode and decode the anonymous
        field: https://play.golang.org/p/SxXP2b-EQC, but it drops the contents of
        Name while encoding.
        2. Even if anonymous fields were not to be supported, I don't think the
        library should panic like that.

    Would be a good idea to file an issue.
    On Tuesday, January 5, 2016 at 9:29:49 PM UTC-8, Larry Clapp wrote:

    This works: http://play.golang.org/p/nUENUMDM7x

    time.Time field is called T

    output is
    "Pythagoras"
    "Treehouse"

    So far so good.

    This doesn't seem to work: http://play.golang.org/p/tooXqJ5Eio

    time.Time field is anonymous

    output is
    ""
    ""

    That's odd.

    This panics with a nill dereference: http://play.golang.org/p/kGoY9QA6fG

    *time.Time field is anonymous (note: pointer to time.Time)

    output is
    panic: runtime error: invalid memory address or nil pointer dereference
    [recovered]
    [...]

    Ouch.

    Am I doing something wrong?

    Thanks!

    -- Larry
    --
    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.
  • Martin Schnabel at Jan 6, 2016 at 6:15 pm
    hi larry,

    when embedding time.Time you will also promote its custom methods
    GobDecode and GobEncode to your types P and Q. if you then try to
    encode and decode using gob the package will see that your types
    implement the GobDecoder and GobEncoder interface and only call those.

    that is why the name fields seems to be ignored.

    the easy solution is not to embed a time.Time or otherwise implement
    the encoding and encoding/gob interfaces to encode and decode your
    types on your own instead of calling the implementation of time.Time.

    hope that helps
    On Di, 2016-01-05 at 21:29 -0800, Larry Clapp wrote:
    This works: http://play.golang.org/p/nUENUMDM7x

    time.Time field is called T

    output is
    "Pythagoras"
    "Treehouse"

    So far so good.

    This doesn't seem to work: http://play.golang.org/p/tooXqJ5Eio

    time.Time field is anonymous

    output is
    ""
    ""

    That's odd.

    This panics with a nill dereference: http://play.golang.org/p/kGoY9QA
    6fG

    *time.Time field is anonymous (note: pointer to time.Time)

    output is
    panic: runtime error: invalid memory address or nil pointer
    dereference [recovered]
    [...]

    Ouch.

    Am I doing something wrong?

    Thanks!

    -- Larry

    --
    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.
  • Larry Clapp at Jan 6, 2016 at 6:33 pm
    Ah, I hadn't thought of that. Thanks!

    In general that behavior is a good thing, but in the specific case of
    gob.Encode it seems like it'd *always* be wrong. Is there some type assert
    that gob.Encode or time.GobEncode could do that could keep it from
    happening? That is, it seems like time.GobEncode should be able to realize
    that it's being asked to encode something that's not a time.Time and panic
    appropriately, or something.

    Or maybe it's not always wrong, I dunno.

    Anyway, thanks again!

    -- Larry

    On Wednesday, January 6, 2016 at 1:16:11 PM UTC-5, mb0 wrote:

    hi larry,

    when embedding time.Time you will also promote its custom methods
    GobDecode and GobEncode to your types P and Q. if you then try to
    encode and decode using gob the package will see that your types
    implement the GobDecoder and GobEncoder interface and only call those.

    that is why the name fields seems to be ignored.

    the easy solution is not to embed a time.Time or otherwise implement
    the encoding and encoding/gob interfaces to encode and decode your
    types on your own instead of calling the implementation of time.Time.

    hope that helps
    On Di, 2016-01-05 at 21:29 -0800, Larry Clapp wrote:
    This works: http://play.golang.org/p/nUENUMDM7x

    time.Time field is called T

    output is
    "Pythagoras"
    "Treehouse"

    So far so good.

    This doesn't seem to work: http://play.golang.org/p/tooXqJ5Eio

    time.Time field is anonymous

    output is
    ""
    ""

    That's odd.

    This panics with a nill dereference: http://play.golang.org/p/kGoY9QA
    6fG

    *time.Time field is anonymous (note: pointer to time.Time)

    output is
    panic: runtime error: invalid memory address or nil pointer
    dereference [recovered]
    [...]

    Ouch.

    Am I doing something wrong?

    Thanks!

    -- Larry
    --
    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.
  • Jesse McNelis at Jan 7, 2016 at 12:50 am

    On Thu, Jan 7, 2016 at 5:32 AM, Larry Clapp wrote:
    Ah, I hadn't thought of that. Thanks!

    In general that behavior is a good thing, but in the specific case of
    gob.Encode it seems like it'd always be wrong. Is there some type assert
    that gob.Encode or time.GobEncode could do that could keep it from
    happening? That is, it seems like time.GobEncode should be able to realize
    that it's being asked to encode something that's not a time.Time and panic
    appropriately, or something.
    time.GobEncode is always encoding an time.Time.
    Your case is that gob.Encode is only encoding the time.Time.

    The work around is to have your type implement it's own GobEncode() to
      prevent gob.Encode from seeing the time.Time.GobEncode
    method or you can embed an interface with a
    restricted set of methods instead of the whole time.Time

    --
    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
postedJan 6, '16 at 5:29a
activeJan 7, '16 at 12:50a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase