Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/gob: decode into interface{} directly fails #1271

Closed
rsc opened this issue Nov 15, 2010 · 2 comments
Closed

encoding/gob: decode into interface{} directly fails #1271

rsc opened this issue Nov 15, 2010 · 2 comments
Assignees

Comments

@rsc
Copy link
Contributor

@rsc rsc commented Nov 15, 2010

package main

import (
    "bytes"
    "gob"
    "log"
)

type T int

func main() {
    var buf bytes.Buffer
    enc := gob.NewEncoder(&buf)
    dec := gob.NewDecoder(&buf)
    
    var t T = 2
    if err := enc.Encode(t); err != nil {
        log.Exitf("encode: %v", err)
    }

    var i interface{}
    if err := dec.Decode(&i); err != nil {
        log.Exitf("decode: %v", err)
    }
}

$ 6.out
2010/11/15 15:42:38 decode: gob: wrong type received for local value interface { }
$
@robpike
Copy link
Contributor

@robpike robpike commented Nov 17, 2010

Comment 1:

Gobs do not implement Go assignment rules.  If you want the receiver to decode into an
interface, you must send an interface.  Otherwise the receiver doesn't in general have
the necessary information to store the concrete value.  There are cases, like this one,
in which one could argue that the system should be able to work it out, but in general
it cannot, so that's not the semantics of gobs.

Status changed to WorkingAsIntended.

@alberts
Copy link
Contributor

@alberts alberts commented Nov 19, 2010

Comment 2:

If anybody else comes looking here, here's code that works:
http://groups.google.com/group/golang-nuts/msg/d03880ab22754dc1
@mikioh mikioh changed the title gob: decode into interface{} directly fails encoding/gob: decode into interface{} directly fails Feb 26, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.