1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 22:20:50 +00:00

session:support struct.

gob.Register(v)
This commit is contained in:
astaxie 2014-05-17 00:43:51 +08:00
parent c5c806b58e
commit c265786251
2 changed files with 12 additions and 0 deletions

View File

@ -16,6 +16,7 @@ func Test_gob(t *testing.T) {
a := make(map[interface{}]interface{})
a["username"] = "astaxie"
a[12] = 234
a["user"] = User{"asta", "xie"}
b, err := EncodeGob(a)
if err != nil {
t.Error(err)
@ -33,6 +34,14 @@ func Test_gob(t *testing.T) {
if c[12] != 234 {
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) {

View File

@ -34,6 +34,9 @@ func init() {
}
func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) {
for _, v := range obj {
gob.Register(v)
}
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
err := enc.Encode(obj)