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: Gob tries a level of indirect before giving up #3658

Closed
gopherbot opened this issue May 22, 2012 · 1 comment
Closed

encoding/gob: Gob tries a level of indirect before giving up #3658

gopherbot opened this issue May 22, 2012 · 1 comment

Comments

@gopherbot
Copy link

by AwakenRz:

Before filing a bug, please check whether it has been fixed since the
I think it should already been fixed long ago. But I cannot find the issue tracking
status.

Run "go version" and compare against
go version go1

Thanks.

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1 Run the following program:
package main

import (
    "bytes"
    "encoding/gob"
    "fmt"
)

type P interface {
    Q()
}

type A struct {
    J P
}

type B struct {
    P int
}

func (b *B) Q() {
}

func main() {
    b := B{2}
    c := A{&b}
    gob.Register(A{})
    gob.Register(B{})
    var buffer bytes.Buffer
    en := gob.NewEncoder(&buffer)
    if err := en.Encode(&c); err != nil {
        panic(err.Error())
    }
    buff := bytes.NewBuffer(buffer.Bytes())
    de := gob.NewDecoder(buff)
    if err := de.Decode(&c); err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println(c)
    }
}

What is the expected output?
Should throw no errors

What do you see instead?
A runtime error saying gob: cannot assign value of type main.B to main.P

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
Mac OSX 10.7.4

Which version are you using?  (run 'go version')
go version go1

Please provide any additional information below.
Related discussion can be found 
https://groups.google.com/d/topic/golang-nuts/nl3GMEoT1KU/discussion
https://groups.google.com/d/topic/golang-nuts/2EFErFqpNwI/discussion
@rsc
Copy link
Contributor

rsc commented May 22, 2012

Comment 1:

You need to register the type *B, as that is what implements the interface.
So register &B{} or (*B)(nil).
You don't need to register A at all, since it is not used in interfaces.
http://play.golang.org/p/FO3hNdzRkL

Status changed to WorkingAsIntended.

@mikioh mikioh changed the title Gob tries a level of indirect before giving up encoding/gob: Gob tries a level of indirect before giving up 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
Development

No branches or pull requests

2 participants