session:support struct.
gob.Register(v)
This commit is contained in:
		
							parent
							
								
									34ddcef1dc
								
							
						
					
					
						commit
						237aaadd65
					
				| @ -16,6 +16,7 @@ func Test_gob(t *testing.T) { | |||||||
| 	a := make(map[interface{}]interface{}) | 	a := make(map[interface{}]interface{}) | ||||||
| 	a["username"] = "astaxie" | 	a["username"] = "astaxie" | ||||||
| 	a[12] = 234 | 	a[12] = 234 | ||||||
|  | 	a["user"] = User{"asta", "xie"} | ||||||
| 	b, err := EncodeGob(a) | 	b, err := EncodeGob(a) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Error(err) | 		t.Error(err) | ||||||
| @ -33,6 +34,14 @@ func Test_gob(t *testing.T) { | |||||||
| 	if c[12] != 234 { | 	if c[12] != 234 { | ||||||
| 		t.Error("decode int error") | 		t.Error("decode int error") | ||||||
| 	} | 	} | ||||||
|  | 	if c["user"].(User).Username != "asta" { | ||||||
|  | 		t.Error("decode struct error") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type User struct { | ||||||
|  | 	Username string | ||||||
|  | 	NickName string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestGenerate(t *testing.T) { | func TestGenerate(t *testing.T) { | ||||||
|  | |||||||
| @ -34,6 +34,9 @@ func init() { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) { | func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) { | ||||||
|  | 	for _, v := range obj { | ||||||
|  | 		gob.Register(v) | ||||||
|  | 	} | ||||||
| 	buf := bytes.NewBuffer(nil) | 	buf := bytes.NewBuffer(nil) | ||||||
| 	enc := gob.NewEncoder(buf) | 	enc := gob.NewEncoder(buf) | ||||||
| 	err := enc.Encode(obj) | 	err := enc.Encode(obj) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user