Last active
August 29, 2015 14:20
-
-
Save 4ydx/9b55126d313d6bc8a4ce to your computer and use it in GitHub Desktop.
Beware accidentally using non-pointer values when calling register gob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "encoding/gob" | |
| "fmt" | |
| ) | |
| type Animal interface { | |
| Height() int | |
| } | |
| type Man struct { | |
| H int | |
| } | |
| func (m *Man) Height() int { | |
| return m.H | |
| } | |
| type SendMe struct { | |
| Animal Animal | |
| } | |
| func main() { | |
| var m Man | |
| m.H = 99 | |
| var s SendMe | |
| s.Animal = &m | |
| // do not do this | |
| // gob.Register(Man{}) | |
| // do not do this | |
| gob.Register(&Man{}) | |
| var network bytes.Buffer | |
| enc := gob.NewEncoder(&network) | |
| dec := gob.NewDecoder(&network) | |
| err := enc.Encode(&s) | |
| if err != nil { | |
| panic(err) | |
| } | |
| var s2 SendMe | |
| err = dec.Decode(&s2) | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Printf("%+v %+v %+v\n", m, s2, s2.Animal) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment