func loginHandler(res http.ResponseWriter, req *http.Request, sess sessions.Session) { // Perform authentication logic authenticated := true // Store session data if login is successful if authenticated { sess.Set("isLoggedIn", true) sess.Set("userID", 123) http.Redirect(res, req, "/dashboard", http.StatusSeeOther) } else { http.Redirect(res, req, "/login", http.StatusUnauthorized) } }
func savePreferencesHandler(res http.ResponseWriter, req *http.Request, sess sessions.Session) { // Get form data theme := req.FormValue("theme") fontSize := req.FormValue("fontSize") // Store preferences in session sess.Set("theme", theme) sess.Set("fontSize", fontSize) http.Redirect(res, req, "/preferences", http.StatusSeeOther) }This example demonstrates how to use the Session Set function to store user preferences for a web application. The user selects their preferred theme and font size, which is saved in the session. This data can then be used to customize the application's appearance and functionality. Overall, the Session Set function is an essential tool for managing user sessions in web applications. By using this function, developers can provide a seamless and personalized experience for their users.