geektutu/7days-golang · error
nil Getter
Error message
nil Getter
What it means
Identical to error 90 but in the day4-consistent-hash version: NewGroup panics with "nil Getter" because the Group was constructed without a cache-miss loader. The library requires a non-nil Getter at construction and fails fast rather than nil-panicking later on a cache miss.
Source
Thrown at gee-cache/day4-consistent-hash/geecache/geecache.go:37
}
// A GetterFunc implements Getter with a function.
type GetterFunc func(key string) ([]byte, error)
// Get implements Getter interface function
func (f GetterFunc) Get(key string) ([]byte, error) {
return f(key)
}
var (
mu sync.RWMutex
groups = make(map[string]*Group)
)
// NewGroup create a new instance of Group
func NewGroup(name string, cacheBytes int64, getter Getter) *Group {
if getter == nil {
panic("nil Getter")
}
mu.Lock()
defer mu.Unlock()
g := &Group{
name: name,
getter: getter,
mainCache: cache{cacheBytes: cacheBytes},
}
groups[name] = g
return g
}
// GetGroup returns the named group previously created with NewGroup, or
// nil if there's no such group.
func GetGroup(name string) *Group {
mu.RLock()
g := groups[name]
mu.RUnlock()View on GitHub (pinned to cf36443821)
Solutions
- Pass a valid Getter (e.g. geecache.GetterFunc closure) to NewGroup
- Fix the upstream constructor that returns a typed-nil Getter
- Add a unit test asserting NewGroup is never called with nil in your wiring code
Example fix
// before
geecache.NewGroup("users", 1<<20, nil) // panic: nil Getter
// after
geecache.NewGroup("users", 1<<20, geecache.GetterFunc(func(key string) ([]byte, error) {
return fetchUser(key)
})) Defensive patterns
Strategy: validation
Validate before calling
if getter == nil {
panic("NewGroup requires a non-nil Getter")
}
g := geecache.NewGroup("users", 1<<20, getter) Type guard
func validGetter(g geecache.Getter) bool {
return g != nil
} Try / catch
// Fail fast at startup rather than catching:
if r := recover(); r != nil { log.Fatalf("group init failed: %v", r) } Prevention
- Use geecache.GetterFunc for plain functions so they can never be typed-nil
- Enforce initialization order: DB/client first, groups second
- Assert getters in unit tests for every group your app creates
When it happens
Trigger: geecache.NewGroup(name, cacheBytes, nil) — a nil Getter interface passed directly, or a typed-nil returned from a Getter factory.
Common situations: Getter produced by another subsystem that failed to initialize; variable declared but never assigned; mistaken use of a nil function value instead of a GetterFunc wrapper.
Related errors
AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03).
Data as JSON: /api/errors/a47838c6c4137746.
Report an issue: GitHub.