Search code examples
sessiondictionarygogo-gin

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??


Solution

  • 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)