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

相关推荐
初恋叫萱萱1 分钟前
基于CodeRider-Kilo和MySQL开发一款书店管理系统
数据库·mysql·产品运营
图乐aj4 分钟前
MySQL 运维之日常运维篇 二
运维·mysql
qq_366336379 分钟前
mysql-5.7.38-winx64.zip 启动教程(免安装版)
数据库·mysql·adb
不穿格子的程序员11 分钟前
Redis篇5——Redis深度剖析:系统的“隐形杀手”——热Key与大Key问题
数据库·redis·缓存·热key·大key
·云扬·21 分钟前
深入理解MySQL元数据锁(MDL):原理、问题与实践
数据库·mysql
guoyiguang222 分钟前
mysql in 查询 没有限制1000个,默认是sql大小4M大小
数据库·sql·mysql
IT教程资源D23 分钟前
[N_115]基于springboot,vue教务管理系统
mysql·vue·前后端分离·springboot教务系统
Thexhy25 分钟前
基础篇:Redis核心命令及用法
java·linux·redis
镜花水月linyi26 分钟前
执行SELECT/INSERT/UPDATE/DELETE的SQL语句,MySQL流程是怎么样的?
后端·mysql
luoluoal30 分钟前
基于python的des算法的企业用户数据安全软件(源码+文档)
python·mysql·毕业设计·源码