session:support struct.
gob.Register(v)
Showing
2 changed files
with
12 additions
and
0 deletions
| ... | @@ -16,6 +16,7 @@ func Test_gob(t *testing.T) { | ... | @@ -16,6 +16,7 @@ func Test_gob(t *testing.T) { |
| 16 | a := make(map[interface{}]interface{}) | 16 | a := make(map[interface{}]interface{}) |
| 17 | a["username"] = "astaxie" | 17 | a["username"] = "astaxie" |
| 18 | a[12] = 234 | 18 | a[12] = 234 |
| 19 | a["user"] = User{"asta", "xie"} | ||
| 19 | b, err := EncodeGob(a) | 20 | b, err := EncodeGob(a) |
| 20 | if err != nil { | 21 | if err != nil { |
| 21 | t.Error(err) | 22 | t.Error(err) |
| ... | @@ -33,6 +34,14 @@ func Test_gob(t *testing.T) { | ... | @@ -33,6 +34,14 @@ func Test_gob(t *testing.T) { |
| 33 | if c[12] != 234 { | 34 | if c[12] != 234 { |
| 34 | t.Error("decode int error") | 35 | t.Error("decode int error") |
| 35 | } | 36 | } |
| 37 | if c["user"].(User).Username != "asta" { | ||
| 38 | t.Error("decode struct error") | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | type User struct { | ||
| 43 | Username string | ||
| 44 | NickName string | ||
| 36 | } | 45 | } |
| 37 | 46 | ||
| 38 | func TestGenerate(t *testing.T) { | 47 | func TestGenerate(t *testing.T) { | ... | ... |
| ... | @@ -34,6 +34,9 @@ func init() { | ... | @@ -34,6 +34,9 @@ func init() { |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) { | 36 | func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) { |
| 37 | for _, v := range obj { | ||
| 38 | gob.Register(v) | ||
| 39 | } | ||
| 37 | buf := bytes.NewBuffer(nil) | 40 | buf := bytes.NewBuffer(nil) |
| 38 | enc := gob.NewEncoder(buf) | 41 | enc := gob.NewEncoder(buf) |
| 39 | err := enc.Encode(obj) | 42 | err := enc.Encode(obj) | ... | ... |
-
Please register or sign in to post a comment