{"id":37173389,"url":"https://github.com/kwynto/gosession","last_synced_at":"2026-01-14T20:16:00.455Z","repository":{"id":57708070,"uuid":"499598376","full_name":"Kwynto/gosession","owner":"Kwynto","description":"This is quick session for net/http in golang. This package is perhaps the best implementation of the session mechanism, at least it tries to become one.","archived":false,"fork":false,"pushed_at":"2024-07-31T19:15:59.000Z","size":50,"stargazers_count":259,"open_issues_count":0,"forks_count":18,"subscribers_count":3,"default_branch":"main","last_synced_at":"2026-01-07T03:56:33.029Z","etag":null,"topics":["go","golang","session"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kwynto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-06-03T17:37:13.000Z","updated_at":"2025-12-09T09:34:59.000Z","dependencies_parsed_at":"2024-02-06T01:33:41.348Z","dependency_job_id":null,"html_url":"https://github.com/Kwynto/gosession","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/Kwynto/gosession","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kwynto%2Fgosession","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kwynto%2Fgosession/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kwynto%2Fgosession/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kwynto%2Fgosession/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kwynto","download_url":"https://codeload.github.com/Kwynto/gosession/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kwynto%2Fgosession/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28434422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T18:57:19.464Z","status":"ssl_error","status_checked_at":"2026-01-14T18:52:48.501Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","session"],"created_at":"2026-01-14T20:15:59.489Z","updated_at":"2026-01-14T20:16:00.411Z","avatar_url":"https://github.com/Kwynto.png","language":"Go","readme":"# GoSession\nThis is quick session for net/http in GoLang.  \nThis package is perhaps the best implementation of the session mechanism, at least it tries to become one.  \n\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n[![GoDoc](https://godoc.org/github.com/Kwynto/gosession?status.svg)](https://godoc.org/github.com/Kwynto/gosession)\n[![Go Report Card](https://goreportcard.com/badge/github.com/Kwynto/gosession)](https://goreportcard.com/report/github.com/Kwynto/gosession)\n[![GitHub](https://img.shields.io/github/license/Kwynto/gosession)](https://github.com/Kwynto/gosession/blob/master/LICENSE)\n[![codecov](https://codecov.io/gh/Kwynto/gosession/branch/main/graph/badge.svg?token=TXP2NOMK58)](https://codecov.io/gh/Kwynto/gosession) \n\n**Important note**\nThis package is designed to work with the standard net/http package and has not been tested with other http packages by the developer.\n\n## Contents\n\n- [GoSession](#gosession)\n  - [Contents](#contents)\n  - [What are sessions and why are they needed](#what-are-sessions-and-why-are-they-needed)\n  - [How to connect GoSession](#how-to-connect-gosession)\n  - [How to use GoSession](#how-to-use-gosession)\n  - [Examples of using](#examples-of-using)\n    - [Example 1:](#example-1)\n    - [Example 2:](#example-2)\n    - [Example 3:](#example-3)\n  - [About the package](#about-the-package)\n  - [About the author](#about-the-author)\n\n## What are sessions and why are they needed\nA session on a site is a good method of identifying a site user.  \nA session is often used to authorize a user and retain their identity until the user closes the browser page.  \nWhile the user is working with the site, he saves cookies with a unique identifier, by this identifier one can distinguish one user from another and the server can store special data for a particular user.  \nUser data received during the session period can be used for authorization, marketing and many other cases when it is necessary to collect, process and analyze data about a specific user.  \nA session is an efficient method of interacting with a user.\n\n**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#what-are-sessions-and-why-are-they-needed)**\n\n## How to connect GoSession\nIn your project folder, initialize the Go-module with the command\n\u003e go mod init your_app_name\n\nDownload and install GoSession\n\u003e go get github.com/Kwynto/gosession\n\nNow you can add the GoSession package to your Go-code file, for example in `main.go`\n```go\nimport \"github.com/Kwynto/gosession\"\n```\n\n**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#how-to-connect-gosession)**\n\n## How to use GoSession\nTo use the GoSession package, you need to import it into your code.\n```go\nimport \"github.com/Kwynto/gosession\"\n```\n\nAll operations for working with sessions must be called from handlers.  \nEach time you start working with the session store, you need to call `gosession.Start(w *http.ResponseWriter, r *http.Request)`, since this function returns the identifier of the store and allows you to access the elements of the store through the identifier.\n```go\nid := gosession.Start(\u0026w, r)\n```\n\nYou need to call the `gosession.Start(w *http.ResponseWriter, r *http.Request)` function from the handler\n```go\nfunc rootHandler(w http.ResponseWriter, r *http.Request) {\n  id := gosession.Start(\u0026w, r) // Get the storage ID for a specific user\n\n  html := \"\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eTitle\u003c/title\u003e\u003c/head\u003e\u003cbody\u003e%s\u003c/body\u003e\u003c/html\u003e\"\n  fmt.Fprintf(w, html, id)\n}\n```\n\nAlternatively, you can use the `gosession.StartSecure(w *http.ResponseWriter, r *http.Request)` function instead of `gosession.Start(w, r)`.  \nThe `StartSecure()` function replaces the session ID each time it is accessed, which reduces the possibility of ID hijacking.  \nThe use of these functions is exactly the same.  \n```go\nid := gosession.StartSecure(\u0026w, r)\n```\n\nYou need to call the `gosession.StartSecure()` function from the handler  \n```go\nfunc rootHandler(w http.ResponseWriter, r *http.Request) {\n  id := gosession.StartSecure(\u0026w, r) // Get the storage ID for a specific user\n\n  html := \"\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eTitle\u003c/title\u003e\u003c/head\u003e\u003cbody\u003e%s\u003c/body\u003e\u003c/html\u003e\"\n  fmt.Fprintf(w, html, id)\n}\n```\n\nOnce you have a store ID, you can write variables to the store, read them, and delete them.\n\nRecording is done using the `Set(name string, value interface{})` method\n```go\nid.Set(\"name variable\", anyVariable)\n```\n\nIn the handler it looks like this\n```go\nfunc writeHandler(w http.ResponseWriter, r *http.Request) {\n  name := \"username\"\n  username := \"JohnDow\"\n\n  id := gosession.Start(\u0026w, r)\n  id.Set(name, username)\n\n  html := \"\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eTitle\u003c/title\u003e\u003c/head\u003e\u003cbody\u003eOK\u003c/body\u003e\u003c/html\u003e\"\n  fmt.Fprint(w, html)\n}\n```\n\nReading is done by `Get(name string) interface{}` method for one variable  \nand the `GetAll() Session` method to read all session variables\n```go\nanyVariable := id.Get(\"name variable\")\n```\n\n```go\nallVariables := id.GetAll()\n```\n\nIn the handler it looks like this\n```go\nfunc readHandler(w http.ResponseWriter, r *http.Request) {\n  name := \"username\"\n  var username interface{}\n\n  id := gosession.Start(\u0026w, r)\n  username = id.Get(name) // Reading the \"username\" variable from the session for a specific user\n\n  html := \"\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eTitle\u003c/title\u003e\u003c/head\u003e\u003cbody\u003e%s\u003c/body\u003e\u003c/html\u003e\"\n  fmt.Fprintf(w, html, username)\n}\n```\n\nor so\n```go\nfunc readHandler(w http.ResponseWriter, r *http.Request) {\n  var tempStr string = \"\"\n\n  id := gosession.Start(\u0026w, r)\n  allVariables := id.GetAll() // Reading the entire session for a specific client\n\n  for i, v := range allVariables {\n    tempStr = fmt.Sprint(tempStr, i, \"=\", v, \"\u003cbr\u003e\")\n  }\n  html := \"\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eTitle\u003c/title\u003e\u003c/head\u003e\u003cbody\u003e%s\u003c/body\u003e\u003c/html\u003e\"\n  fmt.Fprintf(w, html, tempStr)\n}\n```\n\nRemoving an entry from a session of a specific client is carried out using the `Remove(name string)` method\n```go\nid.Remove(\"name variable\")\n```\n\nIn the handler it looks like this\n```go\nfunc removeHandler(w http.ResponseWriter, r *http.Request) {\n  id := gosession.Start(\u0026w, r)\n  id.Remove(\"name variable\") // Removing a variable from a specific client session\n\n  html := \"\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eTitle\u003c/title\u003e\u003c/head\u003e\u003cbody\u003eOK\u003c/body\u003e\u003c/html\u003e\"\n  fmt.Fprint(w, html)\n}\n```\n\nRemoving the entire session of a specific client is done using the `Destroy(w *http.ResponseWriter)` method\n```go\nid.Destroy(\u0026w)\n```\n\nIn the handler it looks like this\n```go\nfunc destroyHandler(w http.ResponseWriter, r *http.Request) {\n  id := gosession.Start(\u0026w, r)\n  id.Destroy(\u0026w) // Deleting the entire session of a specific client\n\n  html := \"\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eTitle\u003c/title\u003e\u003c/head\u003e\u003cbody\u003eOK\u003c/body\u003e\u003c/html\u003e\"\n  fmt.Fprint(w, html)\n}\n```\n\nGoSession allows you to change its settings with the `SetSettings(setings GoSessionSetings)` function,  \nwhich is used outside of the handler, for example, inside the `main()` function\n```go\nvar mySetingsSession = gosession.GoSessionSetings{\n  CookieName:    gosession.GOSESSION_COOKIE_NAME,\n  Expiration:    gosession.GOSESSION_EXPIRATION,\n  TimerCleaning: gosession.GOSESSION_TIMER_FOR_CLEANING,\n}\n\ngosession.SetSetings(mySetingsSession) // Setting session preferences\n```\n\nGoSession has 3 constants available for use\n```go\nconst (\n  GOSESSION_COOKIE_NAME        string        = \"SessionId\" // Name for session cookies\n  GOSESSION_EXPIRATION         int64         = 43_200      // Max age is 12 hours.\n  GOSESSION_TIMER_FOR_CLEANING time.Duration = time.Hour   // The period of launch of the mechanism of cleaning from obsolete sessions\n)\n```\n\nThe remaining functions, types and variables in GoSession are auxiliary and are used only within the package.\n\n**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#how-to-use-gosession)**\n\n## Examples of using\n\n### Example 1:\n*This is a simple authorization example and it shows the use of the write and read session variables functions, as well as deleting the entire session.*\n\nDownload the GoSession project to your computer:\n\u003e git clone https://github.com/Kwynto/gosession.git\n\nGo to the example folder or open this folder in your IDE.\n\u003e cd ./gosession/pkg/example1\n\nInstall GoSession\n\u003e go get github.com/Kwynto/gosession\n\nRun:\n\u003e go mod tidy\n\nStart the server:\n\u003e go run .\n\nVisit site\n\u003e http://localhost:8080/\n\n### Example 2:\n*This example shows a primitive way to collect information about user actions. You can collect any public user data, as well as track user actions, and then save and process this data.*\n\nDownload the GoSession project to your computer:\n\u003e git clone https://github.com/Kwynto/gosession.git\n\nGo to the example folder or open this folder in your IDE.\n\u003e cd ./gosession/pkg/example2\n\nInstall GoSession\n\u003e go get github.com/Kwynto/gosession\n\nRun:\n\u003e go mod tidy\n\nStart the server:\n\u003e go run .\n\nVisit site\n\u003e http://localhost:8080/\n\nNow you can follow the links on this site and see how the site saves and shows your browsing history.\n\n### Example 3:\n*This example shows a simple, realistic site that uses the session mechanism.*\n\nDownload the GoSession project to your computer:\n\u003e git clone https://github.com/Kwynto/gosession.git\n\nGo to the example folder or open this folder in your IDE.\n\u003e cd ./gosession/pkg/example3\n\nInstall GoSession\n\u003e go get github.com/Kwynto/gosession\n\nRun:\n\u003e go mod tidy\n\nStart the server:\n\u003e go run ./cmd/web/\n\nVisit site\n\u003e http://localhost:8080/\n\nNow you can follow the links on this site.\n\n**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#examples-of-using)**\n\n## About the package\n\nGoSession has a description of its functionality in a `README.md` file and internal documentation.  \nGoSession is tested and has a performance check.  \nYou can use the GoSession tests and documentation yourself.\n\nDownload the GoSession project to your computer:\n\u003e git clone https://github.com/Kwynto/gosession.git\n\nGo to the project folder:\n\u003e cd ./gosession\n\n**Check out the documentation**\n\nLook at the documentation in two steps.  \nFirst, in the console, run:\n\u003e godoc -http=:8080\n\nAnd then in your web browser navigate to the uri:\n\u003e http://localhost:8080\n\n*The `godoc` utility may not be present in your Go build and you may need to install it  \ncommand `go get -v golang.org/x/tools/cmd/godoc`*\n\n**For Debian Linux users (Ubuntu, Mint and others):** *You may need to install the tools with the `sudo apt install golang-golang-x-tools` command* \n\nYou can also use Go's standard functionality to view documentation in the console via `go doc`.  \nFor example:  \n\u003e go doc Start\n\nIf your IDE is good enough, then the documentation for functions and methods will be available from your code editor.\n\n**Testing**\n\nRun tests:\n\u003e go test -v\n\nRun tests showing code coverage:\n\u003e go test -cover -v\n\nYou can view code coverage in detail in your web browser.  \nTo do this, you need to sequentially execute two commands in the console:\n\u003e go test -coverprofile=\"coverage.out\" -v  \n\u003e go tool cover -html=\"coverage.out\"\n\n**Performance**\n\nYou can look at code performance tests:\n\u003e go test -benchmem -bench=\".\" gosession.go gosession_test.go\n\n*The slowest of all functions is `cleaningSessions()`, but this should not scare you, as it is a utility function and is rarely executed. This function does not affect the performance of the entire mechanism, it is only needed to clean up the storage from lost sessions.*\n\n**[⬆ back to top](#gosession)** - **[⬆ back to the chapter](#about-the-package)**\n\n## Support the author\n\nYou can support open source projects and the author of this project. The details are [here](https://github.com/Kwynto/Kwynto/blob/main/SUPPORT.md).  \n\n## About the author\n\nThe author of the project is Constantine Zavezeon (Kwynto).  \nYou can contact the author by e-mail: kwynto@mail.ru  \nThe author accepts proposals for participation in open source projects,  \nas well as willing to accept job offers.\nIf you want to offer me a job, then first I ask you to read [this](https://github.com/Kwynto/Kwynto/blob/main/offer.md).\n\n**[⬆ back to top](#gosession)**","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkwynto%2Fgosession","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkwynto%2Fgosession","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkwynto%2Fgosession/lists"}