Gorilla Sessions and Maps

140 views
Skip to first unread message

jackman

unread,
Oct 9, 2012, 11:14:28 AM10/9/12
to goril...@googlegroups.com
Here is the code about which I speak:

The problem I am experiencing is that when I try to store a map into the session, I get the following error:
securecookie: the value could not be encoded

The map I'm trying to assign to the session is as follows (with "%#v"):
map[string]int{"073366118239":1, "073366118238":2}

I've been beating my head against the wall trying to figure out why this isn't working now, but it was earlier. I even have residual session data in the same session. The only thing that I've changed recently is this line:
cart := make(map[string]int) // map[UPC]Quantity

It was originally just the following:
var cart map[string]int

Go was complaining about making assignments to a nil map.

I haven't changed the CookieStore key.

Here is the complete handler:

Thanks for all your help!

Andrew Jackman.


Kamil Kisiel

unread,
Oct 9, 2012, 2:35:21 PM10/9/12
to goril...@googlegroups.com
Session uses securecookie to encode values in to the session. securecookie uses gob under the hood, the actual error is that your map type is not registered with gob. 

You need to make a function like:

func init() {
    gob.Register(map[string]int{})
}

in your package to register your type with Gob. Unfortunately the reason it failed is unclear because of https://github.com/gorilla/securecookie/issues/1

jackman

unread,
Oct 10, 2012, 7:01:11 AM10/10/12
to goril...@googlegroups.com
May the Gods of Code smile kindly upon you, sir!

Rodrigo Moraes

unread,
Oct 13, 2012, 4:32:55 AM10/13/12
to Gorilla web toolkit
On Oct 9, 3:35 pm, Kamil Kisiel wrote:
> Unfortunately the reason it
> failed is unclear because
> of https://github.com/gorilla/securecookie/issues/1

Just closed it. This has been a common cause of confusion since
beginning. :)

-- rodrigo

Reply all
Reply to author
Forward
0 new messages