golang session实现库 支持cookie, redis,mysql等多种存储方式

golang中官方是不支持session的, 如果想要实现session则需要自己动手来实现,或者使用第三方的go-session实现库, 今天就给大家介绍一个go语言的第三方session实现库 go-sessions,支持 的存储方式有 cookie, file, redis, mysql等众多的存储。

使用示例:

Go 复制代码
import (
		"net/http"
		"github.com/gorilla/sessions"
	)

	// Note: Don't store your key in your source code. Pass it via an
	// environmental variable, or flag (or both), and don't accidentally commit it
	// alongside your code. Ensure your key is sufficiently random - i.e. use Go's
	// crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
	var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))

	func MyHandler(w http.ResponseWriter, r *http.Request) {
		// Get a session. We're ignoring the error resulted from decoding an
		// existing session: Get() always returns a session, even if empty.
		session, _ := store.Get(r, "session-name")
		// Set some session values.
		session.Values["foo"] = "bar"
		session.Values[42] = 43
		// Save it before we write to the response/return from the handler.
		err := session.Save(r, w)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

Github项目地址:

GitHub - tekintian/go-sessions: go语言里面的Sessions实现库, 支持众多的Session存储方式,cookie, redis, mysql等。 Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.go语言里面的Sessions实现库, 支持众多的Session存储方式,cookie, redis, mysql等。 Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends. - tekintian/go-sessionshttps://github.com/tekintian/go-sessions

相关推荐
dovens1 分钟前
MYSQL批量UPDATE的两种方式
数据库·mysql
阿杜杜不是阿木木5 分钟前
authentik开源身份认证与管理平台-介绍与安装(1)
数据库·redis·开源·authing·sso·authentik
of Watermelon League33 分钟前
Redis 通用命令
前端·redis·bootstrap
田超凡34 分钟前
深入理解MySQL_8 Index 索引(上)
mysql·java-ee
会飞的大可9 小时前
Redis Sentinel 高可用方案在WMS仓储管理系统的应用
redis·sentinel
小羊在睡觉10 小时前
Reids缓存穿透、击穿、雪崩
redis·缓存·go
J超会运10 小时前
OpenEuler系统MySQL故障排查终极指南
mysql
ward RINL12 小时前
Redis 安装及配置教程(Windows)【安装】
数据库·windows·redis
Nontee13 小时前
Redis高可用架构解析
数据库·redis·架构
csdn_aspnet13 小时前
MySQL主从延迟根因诊断法,从网络、IO、SQL到参数,系统化定位高并发下的同步瓶颈
数据库·mysql·主从