Skip to content

Instantly share code, notes, and snippets.

@4ydx
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save 4ydx/9b55126d313d6bc8a4ce to your computer and use it in GitHub Desktop.

Select an option

Save 4ydx/9b55126d313d6bc8a4ce to your computer and use it in GitHub Desktop.
Beware accidentally using non-pointer values when calling register gob
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