Go session variables?

Ross Salas picture Ross Salas · Jun 7, 2012 · Viewed 12.7k times · Source

I'm new to the Go language (Golang) and I'm writing a web-based application. I'd like to use session variables, like the kind in PHP (variables that are available from one page to the next and unique for a user session). Is there something like that in Go? If not, how would I go about implementing them myself? Or what alternatives methods are there?

Answer

Jeremy Wall picture Jeremy Wall · Jun 7, 2012

You probably want to take a look at gorilla. It has session support as documented here.

Other than that or possibly one of the other web toolkits for go you would have to roll your own.

Possible solutions might be:

  • goroutine per user session to store session variables in memory.
  • store your variables in a session cookie.
  • use a database to store user session data.

I'll leave the implementation details of each of those to the reader.