Code Room
Code reviewMedium
Question
Review this Go in-memory cache. Two goroutines may call GetOrCompute for the same key concurrently.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.
Learn the concepts
type Cache struct { mu sync.RWMutex data map[string]int} func (c *Cache) GetOrCompute(key string, compute func() int) int { c.mu.RLock() v, ok := c.data[key] c.mu.RUnlock() if ok { return v } val := compute() c.mu.Lock() c.data[key] = val c.mu.Unlock() return val}Run or narrate your approach, then ask the coach.