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

相关推荐
Java水解23 分钟前
MySQL 慢查询 debug:索引没生效的三重陷阱
后端·mysql
星光一影1 小时前
Java版社交系统/聊天系统/im/即时通信/社交通讯
java·spring boot·mysql·交友
pipip.1 小时前
Go原生高性能内存网关IMS,比Redis更快
开发语言·redis·golang
程序员卷卷狗3 小时前
MySQL 慢查询优化:从定位、分析到索引调优的完整流程
android·mysql·adb
q***06294 小时前
环境安装与配置:全面了解 Go 语言的安装与设置
开发语言·后端·golang
CodeLongBear5 小时前
MySQL进阶学习笔记:从单表查询到多表关联的深度解析(万字详解)
笔记·学习·mysql
运维_攻城狮5 小时前
openeuler-24.3欧拉系统mysql开机自启报错
linux·mysql
修行者Java5 小时前
Redis 核心功能全解析:功能描述、应用场景与 Java 实战
redis
xie_pin_an5 小时前
Redis 核心命令速查表
数据库·redis·缓存
魏 无羡5 小时前
windows 安装mysql(多个版本同时安装)
windows·mysql·adb