Overview
Build an in-memory session store in Go with a map and methods to set, get, delete and count sessions, using the comma-ok idiom to tell a missing token from an empty value.
- v, ok := m[k] tells missing from zero
- delete(m, k)
- a constructor returns *Store
Short Course
Read a map with two values, v, ok := m[k]: ok says whether the key exists.
Trap: m[k] alone returns the zero value for a missing key, so a stored empty
string looks like no key at all.
roles := map[string]string{"ana": ""}
r, ok := roles["ana"] // "", true
r, ok = roles["bo"] // "", false
delete(roles, "ana")
Task
Write NewStore() *Store and the methods Set(token, user string),
Get(token string) (string, bool), Delete(token string) and Count() int.
Example: after Set("t1", "ana"), Get("t1") → ana, true.
Test Suites
Pending
Next
Output