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

相关推荐
哈哈真棒2 小时前
sparkSQL读入csv文件写入mysql(2)
数据库·mysql
Cynicism_Smile2 小时前
Mysql 8.0.32 union all 创建视图后中文模糊查询失效
数据库·mysql
Aurora_NeAr3 小时前
Redis设计与实现——Redis命令参考与高级特性
数据库·redis·缓存
程序猿小谢3 小时前
Redis特性与应用
数据库·redis·缓存
2401_836836593 小时前
mysql集群
mysql·adb
编程、小哥哥3 小时前
Java求职者面试:从Spring Boot到微服务的技术点解析
java·spring boot·redis·微服务·spring security·高并发·面试题
ZHOU_WUYI3 小时前
MySQL 与 FastAPI 交互教程
mysql·fastapi
Code哈哈笑3 小时前
【基于Spring Boot 的图书购买系统】深度讲解 用户注册的前后端交互,Mapper操作MySQL数据库进行用户持久化
数据库·spring boot·后端·mysql·mybatis·交互
{⌐■_■}3 小时前
【kafka】kafka概念,使用技巧go示例
golang·kafka·linq
Nuraliye3 小时前
IP到国家代码映射之GeoLite2导入到MySQL形成数据字典
mysql·geolite2·geoip_country_blocks·geoip_country_locations·ip到国家代码映射