Go · Level 2 Project

Project: Session Store

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

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")

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.

solution.go
Test Suites Pending
Output