Set in session.Values on map

hi,

I want to set a navi as map() into the session.Values(). I use the sessions from groilla/sessions

session.Values["test"] = map[string]int{"A": 1, "B": 2, "C": 3}

I get not error output. What is my problem?

Please show your whole program. Thanks.

I can’t show your “whole” program, this ist too mutch.

My function to set Session:


package main

import (
	"database/sql"
	"github.com/gorilla/sessions"

)

func setSession(id int, response http.ResponseWriter, request *http.Request) {

			session, err := sessionStore.Get(request, SessionName)
			if err != nil {
				handleSessionError(response, err)
				return
			}

			session.Options = &sessions.Options{
				//Domain:   "localhost",
				Path:   "/",
				MaxAge: 3600,
				//Secure:   false,
				HttpOnly: true,
			}


			session.Values["name"] = "test"
			session.Values["test"] = getProjectNavi(1)

			if err := session.Save(request, response); err != nil {
				handleSessionError(response, err)
				return
			}

		}

	}
}

func getProjectNavi(oid int) (menu map[string]string) {

	rows, err := db.Query("SELECT pid, name, farbe FROM projecte WHERE oid=? AND del=0", oid)
	checkError(err)

	defer rows.Close()

	m := map[string]string{}

	for rows.Next() {

		var pid int
		var name string
		var farbe string
		rows.Scan(&pid, &name, &farbe)

		m[name] = getIdToString(pid)

	}

	return m

}

without the map() it works the sessions.

Can you write a small test program with one handler that sets these session keys. If that works, then go back and add the extra database stuff

session.Values["name"] = "test"
session.Values["test"] = map[string]string { "hello": "world" }

Also, always check errors

If this fails then you would be doing

m[""] = getIdToString(0)

the functions, getIdToString(), getProjectNavi() ect. runs fine.

when I put a map() into session.Values logs of handleSessionError

I think the ‘session.Values’ can’t work with map(), but that would be extremely unfavorable.

an excerpt from the session package

// Session stores the values and optional configuration for a session.
type Session struct {
	ID      string
	Values  map[interface{}]interface{}
	Options *Options
	IsNew   bool
	store   Store
	name    string
}

What is the text of the error that is returned?

securecookie: error - caused by: securecookie: error - caused by: gob: type not registered for interface: map[string]string

Yup, it looks like gorilla/session cannot store map's. This does not appear to be documented. The best place to ask about this would be their issue tracker. https://github.com/gorilla/sessions/issues

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.