Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set map,struct to session in golang( gin gonic framework)

I am using gin gonic to build a web application. I use https://github.com/gin-gonic/contrib/tree/master/sessions to handle session. Forexample, i set a integer value to session:

function Test(c *gin.Context){
  session:= sessions.Default(c)
  session.Set("mysession",123)
  session.Save()
}

And in another controllers, i can get this session by session.Get("mysession").

But if i set map or struct. I only can get the session in the same controller. something wrong here??

like image 824
Vutuz Avatar asked Oct 25 '25 01:10

Vutuz


1 Answers

You probably forgot to register it, when your app starts you need to have something like:

package main

import (
    "encoding/gob"
    "path/to/yourpackage"

func init() {
    gob.Register(&yourpackage.YourStruct{})
}

You can look here http://www.gorillatoolkit.org/pkg/sessions for more information (gin-gonic uses gorilla sessions under the hood)

like image 56
dave Avatar answered Oct 26 '25 17:10

dave