gorilla/sessions

[question] response always set-cookie

marktohark opened this issue · 2 comments

Describe the problem you're having
every response reset session.
is this normal?

未命名
未命名
未命名

Versions
go version go1.12.5 windows/amd64
github.com/gorilla/mux v1.7.3
github.com/gorilla/sessions v1.2.0

"Show me the code!"

package main

import (
	"fmt"
	"net/http"
	"github.com/gorilla/mux"
	"github.com/gorilla/sessions"
)

var store = sessions.NewCookieStore([]byte("something-very-secret"))

func MyHandler(w http.ResponseWriter, r *http.Request) {
	session, err := store.Get(r, "s1")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	fmt.Println("vv => ", session)
	session.Values["name"] = "kingeastensun"
	session.Save(r, w)
}

func MultiSessionHandler(w http.ResponseWriter, r *http.Request) {
	session1, err := store.Get(r, "s1")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	fmt.Println("kk => ", session1)
	session1.Values["name"] = "kingeastensun"

	session2, err := store.Get(r, "s2")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	fmt.Println("kk => ", session2)
	session2.Values["name"] = "kingeastensunyx"
	sessions.Save(r, w)

}

func main() {

	routes := mux.NewRouter()
	routes.HandleFunc("/session", MyHandler)
	routes.HandleFunc("/musession", MultiSessionHandler)

	http.Handle("/", routes)
	http.ListenAndServe(":5655", nil)
}

ok,thanks.