Skip to content

Instantly share code, notes, and snippets.

@shellus
Created September 16, 2017 10:08
Show Gist options
  • Save shellus/fb8806a83808e058fdd081aa147ead0c to your computer and use it in GitHub Desktop.
Save shellus/fb8806a83808e058fdd081aa147ead0c to your computer and use it in GitHub Desktop.
package main
import (
"encoding/gob"
"bytes"
"reflect"
"fmt"
)
type MyStruct struct {
Name string
}
func init(){
gob.Register(MyStruct{})
}
func main(){
// 序列化数据
tmp_io := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(tmp_io)
err := enc.Encode(MyStruct{Name:"shellus"})
if err != nil {
panic(err)
}
// 反序列化数据
MyType := reflect.TypeOf(MyStruct{})
dec := gob.NewDecoder(tmp_io)
MyTypeInstance := reflect.New(MyType)
err = dec.DecodeValue(MyTypeInstance)
if err != nil {
panic(err)
}
// 出错。。。
fmt.Println(MyTypeInstance.Addr().Interface())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment