I want to know how to destroy (delete) a session in gorilla

Asked 2 years ago, Updated 2 years ago, 101 views

http://www.gorillatoolkit.org/pkg/sessions
https://github.com/gorilla/sessions

I use gorilla for session management in Golang.
I'd like to discard all my sessions and then save the data to a new session.
I don't know how to discard the session at that time, so please let me know if anyone knows.

funclearSession1(session*sessions.Session){
    session.Options=&sessions.Options {MaxAge:-1, Path: "/"}
}
/*
  In the first way, the session is not erased immediately.
  be deleted after processing a request
*/

US>funclearSession2(session*sessions.Session){
    session.Values=nil
}
/*
  In the second way, the session can be deleted immediately, but the following error occurs when saving to the session again.
  panic:runtime error:assignment to entry in null map
*/

go

2022-09-30 21:12

1 Answers

After changing session.Options,
session.Save() will do what you want.

funclearSession1 (w http.ResponseWriter, r*http.Requset, session*sessions.Session) {
    session.Options=&sessions.Options {MaxAge:-1, Path: "/"}
    session.Save(r,w)
}


2022-09-30 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.