geektutu/7days-golang · error

nil Getter

Error message

nil Getter

What it means

NewGroup panics with 'nil Getter' when the getter argument is nil. A Group requires a Getter as the on-miss callback that loads a cache value from a backing source (e.g. a database); without it a cache miss could never be resolved. This is a fail-fast programmer-error check, not a recoverable runtime failure.

Source

Thrown at gee-cache/day7-proto-buf/geecache/geecache.go:43

}

// 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},
		loader:    &singleflight.Group{},
	}
	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]

View on GitHub (pinned to cf36443821)

Solutions

  1. Pass a non-nil Getter to NewGroup, e.g. geejdbc.GetterFunc(func(key string) ([]byte, error) {...})
  2. Check program flow that constructs the Getter and ensure it is initialized before NewGroup
  3. If the load is intentionally a no-op, provide a GetterFunc returning an error like errors.New('not found') instead of nil

Example fix

// before
g := geecache.NewGroup("scores", 2<<20, nil)
// after
g := 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 {
    // build a default or fail before calling NewGroup
    getter = geecache.GetterFunc(func(key string) ([]byte, error) {
        return nil, fmt.Errorf("no getter configured for %s", key)
    })
}
g := geecache.NewGroup("scores", 2<<20, getter)

Type guard

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

Prevention

When it happens

Trigger: Calling geecache.NewGroup(name, cacheBytes, nil), e.g. when the Getter is built dynamically and a nil is passed in, or a variable holding the intended Getter is zero-valued.

Common situations: Structuring a Getter behind an interface variable that was never assigned; wiring configuration where the loader is optional but the cache requires it; copy-pasting NewGroup calls and forgetting the getter argument.

Related errors


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