FAQ
I have some elements stored as []interface{}. I can use gob to directly
encode the whole slice and decode it. But if I encode an interface{} and
decode it as an interface{} it shows

Decode failed: gob: local interface type *interface {} can only be decoded from remote interface type; received concrete type int


One testing program is here: http://play.golang.org/p/wQ9VtbSkI5

Did I miss anything? thanks!

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

  • Rob Pike at Jun 30, 2014 at 5:31 pm
    This call
       enc.Encode(vlToWrite[0])
    passes a value of static type interface{} to a function that expects
    static type interface{}, which it then unpacks to see what's inside.
    The result is _exactly_ the same as calling
        enc.Encode(1)
    which "boxes" the 1 into an interface value. Hence you didn't encode
    an interface value, you encoded an integer. The behavior is, in fact,
    exactly the same as how Printf works. If that is unclear to you, read
    this blog post:

       http://blog.golang.org/laws-of-reflection

    If you don't want to understand and just want your code to work, put
    an & before vlToWrite[0], as in http://play.golang.org/p/nPzVnjG-n6.
    That will make sure Encode receives an actual interface value (though
    a pointer) rather than an integer.

    -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.
  • David DENG at Jun 30, 2014 at 6:47 pm
    Thanks Rod! That's a good workaround.

    But is it better dec.Decode can create an instance of the concrete type and
    set it to the interface{} variable? It seems it has all the information it
    needs.

    David
    On Monday, June 30, 2014 10:31:35 AM UTC-7, Rob 'Commander' Pike wrote:

    This call
    enc.Encode(vlToWrite[0])
    passes a value of static type interface{} to a function that expects
    static type interface{}, which it then unpacks to see what's inside.
    The result is _exactly_ the same as calling
    enc.Encode(1)
    which "boxes" the 1 into an interface value. Hence you didn't encode
    an interface value, you encoded an integer. The behavior is, in fact,
    exactly the same as how Printf works. If that is unclear to you, read
    this blog post:

    http://blog.golang.org/laws-of-reflection

    If you don't want to understand and just want your code to work, put
    an & before vlToWrite[0], as in http://play.golang.org/p/nPzVnjG-n6.
    That will make sure Encode receives an actual interface value (though
    a pointer) rather than an integer.

    -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 Jun 30, 2014 at 7:30 pm
    No it does not have all the information it needs. Encode/Decode have
    no idea that it was called with a value of type interface{} rather
    than int. Again, please read the blog post I cited.

    -rob

    On Mon, Jun 30, 2014 at 11:47 AM, David DENG wrote:
    Thanks Rod! That's a good workaround.

    But is it better dec.Decode can create an instance of the concrete type and
    set it to the interface{} variable? It seems it has all the information it
    needs.

    David
    On Monday, June 30, 2014 10:31:35 AM UTC-7, Rob 'Commander' Pike wrote:

    This call
    enc.Encode(vlToWrite[0])
    passes a value of static type interface{} to a function that expects
    static type interface{}, which it then unpacks to see what's inside.
    The result is _exactly_ the same as calling
    enc.Encode(1)
    which "boxes" the 1 into an interface value. Hence you didn't encode
    an interface value, you encoded an integer. The behavior is, in fact,
    exactly the same as how Printf works. If that is unclear to you, read
    this blog post:

    http://blog.golang.org/laws-of-reflection

    If you don't want to understand and just want your code to work, put
    an & before vlToWrite[0], as in http://play.golang.org/p/nPzVnjG-n6.
    That will make sure Encode receives an actual interface value (though
    a pointer) rather than an integer.

    -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.
    --
    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.
  • David DENG at Jun 30, 2014 at 8:15 pm
    The Encode doesn't know but the Encode does.

    The error message shows Encode receives a *interface{} and it knows the
    data to be decoded is a concrete type int. What else information does it
    need?
    On Monday, June 30, 2014 12:30:26 PM UTC-7, Rob 'Commander' Pike wrote:

    No it does not have all the information it needs. Encode/Decode have
    no idea that it was called with a value of type interface{} rather
    than int. Again, please read the blog post I cited.

    -rob


    On Mon, Jun 30, 2014 at 11:47 AM, David DENG <david...@gmail.com
    <javascript:>> wrote:
    Thanks Rod! That's a good workaround.

    But is it better dec.Decode can create an instance of the concrete type and
    set it to the interface{} variable? It seems it has all the information it
    needs.

    David
    On Monday, June 30, 2014 10:31:35 AM UTC-7, Rob 'Commander' Pike wrote:

    This call
    enc.Encode(vlToWrite[0])
    passes a value of static type interface{} to a function that expects
    static type interface{}, which it then unpacks to see what's inside.
    The result is _exactly_ the same as calling
    enc.Encode(1)
    which "boxes" the 1 into an interface value. Hence you didn't encode
    an interface value, you encoded an integer. The behavior is, in fact,
    exactly the same as how Printf works. If that is unclear to you, read
    this blog post:

    http://blog.golang.org/laws-of-reflection

    If you don't want to understand and just want your code to work, put
    an & before vlToWrite[0], as in http://play.golang.org/p/nPzVnjG-n6.
    That will make sure Encode receives an actual interface value (though
    a pointer) rather than an integer.

    -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...@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.
  • David DENG at Jun 30, 2014 at 8:15 pm
    The Encode doesn't know but the Decode does.

    The error message shows Decode receives a *interface{} and it knows the
    data to be decoded is a concrete type int. What else information does it
    need?

    David
    On Monday, June 30, 2014 12:30:26 PM UTC-7, Rob 'Commander' Pike wrote:

    No it does not have all the information it needs. Encode/Decode have
    no idea that it was called with a value of type interface{} rather
    than int. Again, please read the blog post I cited.

    -rob


    On Mon, Jun 30, 2014 at 11:47 AM, David DENG <david...@gmail.com
    <javascript:>> wrote:
    Thanks Rod! That's a good workaround.

    But is it better dec.Decode can create an instance of the concrete type and
    set it to the interface{} variable? It seems it has all the information it
    needs.

    David
    On Monday, June 30, 2014 10:31:35 AM UTC-7, Rob 'Commander' Pike wrote:

    This call
    enc.Encode(vlToWrite[0])
    passes a value of static type interface{} to a function that expects
    static type interface{}, which it then unpacks to see what's inside.
    The result is _exactly_ the same as calling
    enc.Encode(1)
    which "boxes" the 1 into an interface value. Hence you didn't encode
    an interface value, you encoded an integer. The behavior is, in fact,
    exactly the same as how Printf works. If that is unclear to you, read
    this blog post:

    http://blog.golang.org/laws-of-reflection

    If you don't want to understand and just want your code to work, put
    an & before vlToWrite[0], as in http://play.golang.org/p/nPzVnjG-n6.
    That will make sure Encode receives an actual interface value (though
    a pointer) rather than an integer.

    -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...@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.
  • Rob Pike at Jun 30, 2014 at 8:30 pm
    It's a type mismatch. Yes, it could automatically transcode under some
    circumstances but that way leads to madness.

    Instead of having the package intuit your meaning, it's better to send
    what you expect to receive.

    -rob

    On Mon, Jun 30, 2014 at 1:15 PM, David DENG wrote:
    The Encode doesn't know but the Decode does.

    The error message shows Decode receives a *interface{} and it knows the data
    to be decoded is a concrete type int. What else information does it need?

    David
    On Monday, June 30, 2014 12:30:26 PM UTC-7, Rob 'Commander' Pike wrote:

    No it does not have all the information it needs. Encode/Decode have
    no idea that it was called with a value of type interface{} rather
    than int. Again, please read the blog post I cited.

    -rob

    On Mon, Jun 30, 2014 at 11:47 AM, David DENG wrote:
    Thanks Rod! That's a good workaround.

    But is it better dec.Decode can create an instance of the concrete type
    and
    set it to the interface{} variable? It seems it has all the information
    it
    needs.

    David
    On Monday, June 30, 2014 10:31:35 AM UTC-7, Rob 'Commander' Pike wrote:

    This call
    enc.Encode(vlToWrite[0])
    passes a value of static type interface{} to a function that expects
    static type interface{}, which it then unpacks to see what's inside.
    The result is _exactly_ the same as calling
    enc.Encode(1)
    which "boxes" the 1 into an interface value. Hence you didn't encode
    an interface value, you encoded an integer. The behavior is, in fact,
    exactly the same as how Printf works. If that is unclear to you, read
    this blog post:

    http://blog.golang.org/laws-of-reflection

    If you don't want to understand and just want your code to work, put
    an & before vlToWrite[0], as in http://play.golang.org/p/nPzVnjG-n6.
    That will make sure Encode receives an actual interface value (though
    a pointer) rather than an integer.

    -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...@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.
    --
    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
postedJun 30, '14 at 5:17p
activeJun 30, '14 at 8:30p
posts7
users2
websitegolang.org

2 users in discussion

David DENG: 4 posts Rob Pike: 3 posts

People

Translate

site design / logo © 2022 Grokbase