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

相关推荐
骇客野人7 小时前
电商商城微服务架构设计方案(SpringCloud/Go+Vue3+MySQL+ES+RocketMQ)
spring cloud·微服务·golang
码农大叔的博客7 小时前
golang示例:for九九乘法表
开发语言·算法·golang
渣渣盟7 小时前
误删数据之后:MySQL、PostgreSQL、Oracle三库紧急恢复完全实操手册
mysql·postgresql·oracle
布莱克6058 小时前
理解B+树:原理、特性与应用场景
数据结构·数据库·mysql
冰暮流星8 小时前
mysql之排序查询
数据库·mysql
圣殿骑士-Khtangc8 小时前
Go-sync-Pool对象池最佳实践与源码分析
golang
互联网中的一颗神经元8 小时前
01. Go 内存管理全景架构
java·jvm·golang
有脚就行8 小时前
第24篇-Go-gRPC推理服务-高性能跨语言通信
开发语言·人工智能·后端·golang
老鼠拧刀满街找猫8 小时前
Windows Docker部署Redis一主两从+哨兵完整实战(含静态IP方案)
windows·redis·docker
零域码客9 小时前
Redis 双刃剑:缓存与分布式锁的本质区别
redis·分布式·缓存·并发控制·后端架构