简体   繁体   English

将数组结构设置为Golang中的会话

[英]Set array struct to session in golang

I use lib github.com/ipfans/echo-session. 我使用lib github.com/ipfans/echo-session。 I can save session when set array struct 设置数组结构时可以保存会话

This my code: 这是我的代码:

Save session 保存会议

type StaffInfor struct {
    Login_id         string
    Family_name_cc   string
    First_name_cc    string
    Family_name_kana string
    First_name_kana  string
    Role_id      int
    Password     string
    Message_invalid  []string
}
~~~
session := session.Default(c)
session.Set("test", listStaffInfor)// listStaffInfor is array struct StaffInfor 
session.Save()

Get from session 从会话中获取

session := session.Default(c)
fmt.Println(session.Get("test"))

Console result display empty 控制台结果显示为空

Library github.com/ipfans/echo-session is using github.com/gorilla/sessions internally. 图书馆github.com/ipfans/echo-session在内部使用github.com/gorilla/sessions

Gorilla session object is serialised using the encoding/gob package. 大猩猩会话对象使用encoding/gob包进行序列化。 So to store a complex datatype within a session you have to register your struct . 因此,要在会话中存储复杂的数据类型,必须注册struct

type StaffInfor struct {
    Login_id         string
    Family_name_cc   string
    First_name_cc    string
    Family_name_kana string
    First_name_kana  string
    Role_id          int
    Password         string
    Message_invalid  []string
}

type ListStaffInfor []StaffInfor

func init() {
   gob.Register(&StaffInfor{})
   gob.Register(&ListStaffInfor{})
}

Note: If you're using cookie based session, it is not advised to store large object into session, because you might hit cookie size limitation 4KB. 注意:如果您使用的是基于cookie的会话,则不建议将大对象存储到会话中,因为您可能会遇到cookie大小限制为4KB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM