FAQ
I'm trying to write a wrapper around the gob package which is meant to be
used together with Redis. I would like to decode the object into
an interface{} without making a temporary copy of it but it does not seem
to work the way I think it should. To demonstrate the problem I have
attached a minimal program.

The problematic function is get3:

func get3(buffer *bytes.Buffer, v interface{}) {
dec := gob.NewDecoder(buffer)

fmt.Println(reflect.ValueOf(v))
err := dec.Decode(&v)
fmt.Println(reflect.ValueOf(v))
if err != nil {
log.Fatal("decode error:", err)
}
}


When called like follows (with buffer containing the encoded struct &T{1.5,
2.2})

var t4 T

get3(&buffer, &t4)

fmt.Println(t4)


it generates the output

<*main.T Value>
<main.T Value>
{0 0}


I don't understand why Decode changes the type of the interface{} from a
pointer to a value. As you can see in the attached program everything works
well with an intermediate copy which I would like to avoid. How can I fix
this problem to make get3 work as desired?

--
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/groups/opt_out.

Search Discussions

  • Islan Dberry at Feb 10, 2014 at 4:23 pm
    See http://play.golang.org/p/UZeq_kIG7L. There's no need to use reflect.
    On Monday, February 10, 2014 5:13:47 AM UTC-8, Casorati wrote:

    I'm trying to write a wrapper around the gob package which is meant to be
    used together with Redis. I would like to decode the object into
    an interface{} without making a temporary copy of it but it does not seem
    to work the way I think it should. To demonstrate the problem I have
    attached a minimal program.

    The problematic function is get3:

    func get3(buffer *bytes.Buffer, v interface{}) {
    dec := gob.NewDecoder(buffer)

    fmt.Println(reflect.ValueOf(v))
    err := dec.Decode(&v)
    fmt.Println(reflect.ValueOf(v))
    if err != nil {
    log.Fatal("decode error:", err)
    }
    }


    When called like follows (with buffer containing the encoded struct &T{1.5,
    2.2})

    var t4 T

    get3(&buffer, &t4)

    fmt.Println(t4)


    it generates the output

    <*main.T Value>
    <main.T Value>
    {0 0}


    I don't understand why Decode changes the type of the interface{} from a
    pointer to a value. As you can see in the attached program everything works
    well with an intermediate copy which I would like to avoid. How can I fix
    this problem to make get3 work as desired?
    --
    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/groups/opt_out.
  • Islan Dberry at Feb 10, 2014 at 4:48 pm

    On Monday, February 10, 2014 8:23:02 AM UTC-8, Islan Dberry wrote:
    See http://play.golang.org/p/UZeq_kIG7L. There's no need to use reflect.
    Oops, I was a too quick on the reply. It's true that the reflect package is
    not needed, but that is not the problem. The problem is the extra address
    operator in get3.

    The function get3 decodes T to the local variable v. This clobbers whatever
    was in the local variable before calling Decode. The variable in the
    caller to get3 is unmodified.

    If you remove the address operator, then the value is decoded to the
    caller's variable.

    --
    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/groups/opt_out.
  • Kambiz Veshgini at Feb 10, 2014 at 4:52 pm
    Thanks, it works now. I had to remove both extra address operators in
    Encode and Decode.

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 10, '14 at 3:55p
activeFeb 10, '14 at 4:52p
posts4
users2
websitegolang.org

2 users in discussion

Islan Dberry: 2 posts Kambiz Veshgini: 2 posts

People

Translate

site design / logo © 2021 Grokbase