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

相关推荐
计算机毕设指导67 小时前
基于Django的本地健康宝微信小程序系统【源码文末联系】
java·后端·python·mysql·微信小程序·小程序·django
gjc5927 小时前
MySQL无主键大表删除导致主从同步延迟的深度分析
数据库·mysql
汪不止7 小时前
【 分布式唯一业务单号生成方案:Redis + 数据库双保险架构】
数据库·redis·分布式
典孝赢麻崩乐急7 小时前
Redis复习-------Redis事务
数据库·redis·缓存
橘子真甜~7 小时前
Reids命令原理与应用3 - Redis 主线程,辅助线程与存储原理
网络·数据库·redis·缓存·线程·数据类型·存储结构
杨了个杨89827 小时前
Rsyslog + MySQL 实现日志集中存储
数据库·mysql
Token_w7 小时前
Llama 3-8B-Instruct 在昇腾 NPU 上的 SGLang 性能实测
mysql·llama·sglang
思成Codes7 小时前
Go语言的多返回值是如何实现的?
开发语言·后端·golang
better_liang8 小时前
每日Java面试场景题知识点之-MySQL高并发数据一致性保障
mysql·高并发·java面试·数据一致性·企业级开发
Grassto8 小时前
Go 是如何解析 `import path` 的?第三方包定位原理
开发语言·golang·go module·go import