securecookie: error - caused by: gob: name not registered for interface: "map[string]map[int]string"
Problem
Hi! I was trying to run my code, but it appears this error:
securecookie: error - caused by: gob: name not registered for interface: "map[string]map[int]string"I've already tried to register the interface with:gob.Register(map[string](map[int]string){})But I don't know if it's correct
Versions
Go version:
go version go1.18.1 linux/amd64
Code
Here is the part of the code that fails:
var proy_inst map[string](map[int]string)
proy_inst = make(map[string](map[int]string))
for ID_Proyecto, Name_Proyecto := range modelo.GetProyectos_de_Usuario(user.ID) {
proy_inst[Name_Proyecto] = make(map[int]string)
if (proy == modelo.Proyecto{}) {
proy = modelo.GetProyecto_ID(ID_Proyecto)
}
for ID_Instalacion, Name_Instalacion := range modelo.GetInstalaciones_Proyecto(ID_Proyecto) {
proy_inst[Name_Proyecto][ID_Instalacion] = Name_Instalacion
}
}
gob.Register(map[string](map[int]string){})
session.Values["menu"] = proy_inst
session.Values["section"] = "proyecto"
err = session.Save(r, w)
checkErr(err)
Thanks!
Kinda late here, but you have to give your custom type name like so:
type CustomType map[string]map[int]string
Then you can register it:
gob.Register(CustomType{})
Applying it to your snippet would look something like this:
type ProyInst map[string]map[int]string
proy_inst = make(ProyInst)
for ID_Proyecto, Name_Proyecto := range modelo.GetProyectos_de_Usuario(user.ID) {
proy_inst[Name_Proyecto] = make(map[int]string)
if (proy == modelo.Proyecto{}) {
proy = modelo.GetProyecto_ID(ID_Proyecto)
}
for ID_Instalacion, Name_Instalacion := range modelo.GetInstalaciones_Proyecto(ID_Proyecto) {
proy_inst[Name_Proyecto][ID_Instalacion] = Name_Instalacion
}
}
gob.Register(ProyInst{})
session.Values["menu"] = proy_inst
session.Values["section"] = "proyecto"
err = session.Save(r, w)
checkErr(err)
@aroldan00 I am unable to reproduce the issue. I've written a sample code by taking cues from your code snippet. Here's how I have registered a type into gob:
var store = sessions.NewCookieStore([]byte("random"))
func main() {
gob.Register(map[string](map[int]string){})
http.HandleFunc("/hello", MyHandler)
http.ListenAndServe(":8090", nil)
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
var proy_inst map[string](map[int]string)
proy_inst = make(map[string](map[int]string))
proy_inst["hello"] = map[int]string{
1: "1",
}
session.Values["menu"] = proy_inst
session.Values["section"] = "proyecto"
err := session.Save(r, w)
if err != nil {
fmt.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println(session.Values)
}
Output:
map[menu:map[hello:map[1:1]] section:proyecto]
This issue has been open for awhile and it seems like a help requests rather than a bug or feature request, so I'm going to close it out.