我明白,這已經被回答。但是,有關在會話中設置和檢索對象的參考信息,請參閱下面的代碼。
package main
import (
"encoding/gob"
"fmt"
"net/http"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
var store = sessions.NewCookieStore(securecookie.GenerateRandomKey(32))
type Person struct {
FirstName string
LastName string
}
func createSession(w http.ResponseWriter, r *http.Request) {
gob.Register(Person{})
session, _ := store.Get(r, "session-name")
session.Values["person"] = Person{"Pogi", "Points"}
session.Save(r, w)
fmt.Println("Session initiated")
}
func getSession(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
fmt.Println(session.Values["person"])
}
func main() {
http.HandleFunc("/1", createSession)
http.HandleFunc("/2", getSession)
http.ListenAndServe(":8080", nil)
}
您可以通過訪問此:
http://localhost:8080/1 - >會話值設置
http://localhost:8080/2 - >會話值檢索
希望這有助於!
用代碼提交答案請 – OneOfOne
對不起,延誤了。代碼發佈。 – Ben