
Security News
Astral Launches pyx: A Python-Native Package Registry
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
gopkg.in/kataras/go-sessions.v0
Fast, unique & cross-framework http sessions for Go.
Easy to learn, while providing robust set of features.
Ideally suited for both experienced and novice Developers.
import "gopkg.in/kataras/go-sessions.v0"
sess := sessions.Start(http.ResponseWriter, *http.Request)
sess.
ID() string
Get(string) interface{}
HasFlash() bool
GetFlash(string) interface{}
GetFlashString(string) string
GetString(key string) string
GetInt(key string) (int, error)
GetInt64(key string) (int64, error)
GetFloat32(key string) (float32, error)
GetFloat64(key string) (float64, error)
GetBoolean(key string) (bool, error)
GetAll() map[string]interface{}
GetFlashes() map[string]interface{}
VisitAll(cb func(k string, v interface{}))
Set(string, interface{})
SetFlash(string, interface{})
Delete(string)
Clear()
ClearFlashes()
The only requirement is the Go Programming Language, at least v1.7.
$ go get -u gopkg.in/kataras/go-sessions.v0
Take a look at the ./examples.
OUTLINE
// Start starts the session for the particular net/http request
Start(http.ResponseWriter, *http.Request) Session
// Destroy kills the net/http session and remove the associated cookie
Destroy(http.ResponseWriter, *http.Request)
// Start starts the session for the particular valyala/fasthttp request
StartFasthttp(*fasthttp.RequestCtx) Session
// Destroy kills the valyala/fasthttp session and remove the associated cookie
DestroyFasthttp(*fasthttp.RequestCtx)
// UseDatabase ,optionally, adds a session database to the manager's provider,
// a session db doesn't have write access
// see https://github.com/kataras/go-sessions/tree/0.0.7/sessiondb
UseDatabase(Database)
// UpdateConfig updates the configuration field (Config does not receives a pointer, so this is a way to update a pre-defined configuration)
UpdateConfig(Config)
Start
returns a Session
, Session outline
Session interface {
ID() string
Get(string) interface{}
HasFlash() bool
GetFlash(string) interface{}
GetString(key string) string
GetFlashString(string) string
GetInt(key string) (int, error)
GetInt64(key string) (int64, error)
GetFloat32(key string) (float32, error)
GetFloat64(key string) (float64, error)
GetBoolean(key string) (bool, error)
GetAll() map[string]interface{}
GetFlashes() map[string]interface{}
VisitAll(cb func(k string, v interface{}))
Set(string, interface{})
SetFlash(string, interface{})
Delete(string)
Clear()
ClearFlashes()
}
package main
import (
"fmt"
"gopkg.in/kataras/go-sessions.v0"
"net/http"
)
func main() {
// set some values to the session
setHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
values := map[string]interface{}{
"Name": "go-sessions",
"Days": "1",
"Secret": "dsads£2132215£%%Ssdsa",
}
sess := sessions.Start(res, req) // init the session
// sessions.Start returns the Session interface we saw before
for k, v := range values {
sess.Set(k, v) // fill session, set each of the key-value pair
}
res.Write([]byte("Session saved, go to /get to view the results"))
})
http.Handle("/set/", setHandler)
// get the values from the session
getHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
sess := sessions.Start(res, req) // init the session
sessValues := sess.GetAll() // get all values from this session
res.Write([]byte(fmt.Sprintf("%#v", sessValues)))
})
http.Handle("/get/", getHandler)
// clear all values from the session
clearHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
sess := sessions.Start(res, req)
sess.Clear()
})
http.Handle("/clear/", clearHandler)
// destroys the session, clears the values and removes the server-side entry and client-side sessionid cookie
destroyHandler := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
sessions.Destroy(res, req)
})
http.Handle("/destroy/", destroyHandler)
fmt.Println("Open a browser tab and navigate to the localhost:8080/set/")
http.ListenAndServe(":8080", nil)
}
StartFasthttp
returns again Session
, Session outline
Session interface {
ID() string
Get(string) interface{}
HasFlash() bool
GetFlash(string) interface{}
GetString(key string) string
GetFlashString(string) string
GetInt(key string) (int, error)
GetInt64(key string) (int64, error)
GetFloat32(key string) (float32, error)
GetFloat64(key string) (float64, error)
GetBoolean(key string) (bool, error)
GetAll() map[string]interface{}
GetFlashes() map[string]interface{}
VisitAll(cb func(k string, v interface{}))
Set(string, interface{})
SetFlash(string, interface{})
Delete(string)
Clear()
ClearFlashes()
}
package main
import (
"fmt"
"gopkg.in/kataras/go-sessions.v0"
"github.com/valyala/fasthttp"
)
func main() {
// set some values to the session
setHandler := func(reqCtx *fasthttp.RequestCtx) {
values := map[string]interface{}{
"Name": "go-sessions",
"Days": "1",
"Secret": "dsads£2132215£%%Ssdsa",
}
sess := sessions.StartFasthttp(reqCtx) // init the session
// sessions.StartFasthttp returns the, same, Session interface we saw before too
for k, v := range values {
sess.Set(k, v) // fill session, set each of the key-value pair
}
reqCtx.WriteString("Session saved, go to /get to view the results")
}
// get the values from the session
getHandler := func(reqCtx *fasthttp.RequestCtx) {
sess := sessions.StartFasthttp(reqCtx) // init the session
sessValues := sess.GetAll() // get all values from this session
reqCtx.WriteString(fmt.Sprintf("%#v", sessValues))
}
// clear all values from the session
clearHandler := func(reqCtx *fasthttp.RequestCtx) {
sess := sessions.StartFasthttp(reqCtx)
sess.Clear()
}
// destroys the session, clears the values and removes the server-side entry and client-side sessionid cookie
destroyHandler := func(reqCtx *fasthttp.RequestCtx) {
sessions.DestroyFasthttp(reqCtx)
}
fmt.Println("Open a browser tab and navigate to the localhost:8080/set")
fasthttp.ListenAndServe(":8080", func(reqCtx *fasthttp.RequestCtx) {
path := string(reqCtx.Path())
if path == "/set" {
setHandler(reqCtx)
} else if path == "/get" {
getHandler(reqCtx)
} else if path == "/clear" {
clearHandler(reqCtx)
} else if path == "/destroy" {
destroyHandler(reqCtx)
} else {
reqCtx.WriteString("Please navigate to /set or /get or /clear or /destroy")
}
})
}
If you'd like to discuss this package, or ask questions about it, feel free to
Current: v0.0.7
Read more about Semantic Versioning 2.0.0
The author of go-sessions is @kataras.
If you are interested in contributing to the go-sessions project, please make a PR.
This project is licensed under the MIT License.
License can be found here.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.