Session Redis会话映射不允许索引
我有一个web应用程序,它使用会话结构和组合模型结构Session Redis会话映射不允许索引,session,go,struct,redis,hashmap,Session,Go,Struct,Redis,Hashmap,我有一个web应用程序,它使用会话结构和组合模型结构 type Session struct { SessionModel } type SessionModel interface { } UserSession是由会话 type UserSession struct { Name string Email string Password string } 当我从Redis解组JSON会话数据时,一个映射存储在SessionModel字段(允许类嵌入的接口)
type Session struct {
SessionModel
}
type SessionModel interface { }
UserSession是由会话
type UserSession struct {
Name string
Email string
Password string
}
当我从Redis解组JSON会话数据时,一个映射存储在SessionModel字段(允许类嵌入的接口)中
我可以访问SessionModel,它的类型是一个映射,但是当我尝试访问索引值时,会出现错误无效操作:session.SessionModel[“Username”](type sessions.SessionModel不支持索引)
。发生了什么事
reflect.TypeOf(sessions.SessionModel) //map[string]interface {}
session, err := SessionInstance.Get(sessions.Session{sessions.UserSession{}}, cookie.Value)
username := sessions.SessionModel["Username"] //invalid operation: session.SessionModel["Username"] (type sessions.SessionModel does not support indexing)
您是否尝试将
r SessionStore
作为指针r*SessionStore
?我做了,但没有做任何事情。`SessionModel及其类型是一个map`。没有。SessionModel是接口{}
的类型,在当前示例中,它包含一个映射,而不是系统性的。因此,在尝试将其用作映射之前,必须首先执行类型断言。我该如何做呢?另外,我说SessionModel是一个映射,因为根据reflect.TypeOf函数,SessionModel就是这样的。
reflect.TypeOf(sessions.SessionModel) //map[string]interface {}
session, err := SessionInstance.Get(sessions.Session{sessions.UserSession{}}, cookie.Value)
username := sessions.SessionModel["Username"] //invalid operation: session.SessionModel["Username"] (type sessions.SessionModel does not support indexing)