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

相关推荐
幽月静语9 分钟前
MYSQL双主双从,使用Keepalived双机热备+LVS高可用群集
mysql
关中雪18 分钟前
【应届应知应会】SQL常用知识点50道
数据库·mysql·nosql·秋招·校招·春招
zxrhhm29 分钟前
MySQL中使用PROFILING来查看SQL执行流程
数据库·mysql
清风撩耳ing1 小时前
MySQL之MHA高可用集群及故障切换
mysql·adb
关兮月2 小时前
MySQL存储与优化 一、MySQL架构原理
数据库·mysql
JasonHome2 小时前
ElasticSearch与MySQL概念对比
mysql·elasticsearch
风中的默默2 小时前
《深入浅出MySQL:数据库开发、优化与管理维护(第3版)》
数据库·mysql·数据库开发
Architect_Lee2 小时前
Redis 客户端命令大全
redis
虫小宝2 小时前
Spring Boot中集成MySQL数据库的步骤和技巧
数据库·spring boot·mysql
java一只鱼2 小时前
基于Spring Boot的先进时尚室内管理系统
spring boot·mysql·elementui·vue·layui