geektutu/7days-golang · critical

nil Getter

Error message

nil Getter

What it means

geecache.NewGroup panics when the provided cache-miss callback (Getter) is nil, because the group would have no way to fetch values on a cache miss. The library treats this as a programmer error, so it fails immediately at group construction rather than at first Get.

Source

Thrown at gee-cache/day2-single-node/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

  1. Pass a non-nil Getter implementation (e.g. a function wrapped via GetterFunc) to NewGroup.
  2. Guard the getter variable before calling NewGroup and return a configuration error instead.
  3. Wire a real data-source callback (database, API, etc.) used on cache misses.

Example fix

// before
var getter gee Getter // nil
geecache.NewGroup("scores", 2<<20, getter)
// after
geecache.NewGroup("scores", 2<<20, geecache.GetterFunc(func(key string) ([]byte, error) {
    return loadFromDB(key)
}))
Defensive patterns

Strategy: validation

Validate before calling

if getter == nil {
    return errors.New("geecache: NewGroup requires a non-nil Getter")
}
g := geecache.NewGroup(name, cacheBytes, getter)

Type guard

func hasGetter(g geecache.Getter) bool { return g != nil }
if !hasGetter(getter) { getter = defaultGetter }

Try / catch

// NewGroup panics, so guard before calling; if unavoidable:
func safeNewGroup(name string, bytes int64, getter geecache.Getter) (g *geecache.Group) {
    defer func() {
        if r := recover(); r != nil && r == "nil Getter" {
            g = geecache.NewGroup(name, bytes, geecache.GetterFunc(defaultLoader))
        }
    }()
    return geecache.NewGroup(name, bytes, getter)
}

Prevention

When it happens

Trigger: NewGroup(name, cacheBytes, nil) — typically when the getter is a variable that is nil at construction time, or when building groups programmatically where a callback was omitted.

Common situations: Conditional getter wiring where a DB/etcd callback failed to initialize; copy-pasted createGroup code passing a nil interface; refactors that removed the fetch function but kept NewGroup.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/b24e86a78512131a. Report an issue: GitHub.