Created
September 16, 2017 10:08
-
-
Save shellus/fb8806a83808e058fdd081aa147ead0c to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"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