package main import ( "net/http" "github.com/gorilla/sessions" ) func main() { // Create a new cookie store store := sessions.NewCookieStore([]byte("my-secret-key")) // Set a session value r, _ := http.NewRequest("GET", "/", nil) w := httptest.NewRecorder() session, _ := store.Get(r, "session-name") session.Values["key"] = "value" session.Save(r, w) }
package main import ( "fmt" "net/http" "github.com/gorilla/sessions" ) func main() { // Create a new cookie store store := sessions.NewCookieStore([]byte("my-secret-key")) // Get a session value r, _ := http.NewRequest("GET", "/", nil) r.AddCookie(&http.Cookie{Name: "session-name", Value: "MTYxNjQ4MTUwOXxEdi1CQkFFQ180SUFBUkFCRUFBQVFVTV9fMjAxNjA0MjVUMTExNjEwWi1jQnJYVDgxMUNlUGZBdVRINkZqV0tMQWxTb05xenlmLXFuaGZsVlFKMS1Wb29ZTmhoSmpDbzYtaV81cGVSUkpmbVEZebRrL0h5JkVEvOXnVCupxQlyWMLNMNv1BD1U8WsIABa9fNUhnjtlAk7u5xlB-osx8dMyDg=="}) w := httptest.NewRecorder() session, _ := store.Get(r, "session-name") myValue := session.Values["key"] fmt.Println(myValue) }Example 2 shows how to get a session value from a cookie using the `http.Request` and `http.ResponseWriter`. The `r.AddCookie` function is used to add the session cookie to the request. The value is then printed to the console. Overall, the `github.com.gorilla.sessions` package library provides a simple and effective way to manage sessions in Go web applications, using cookies to store session data.