gohugoio/hugo · error
len of nil pointer
Error message
len of nil pointer
What it means
Raised by the `len` template function when its argument, after `indirect`, is a nil pointer (funcs.go:293-295). `len` cannot measure something that does not exist; a nil `*[]T`/`*string`/`*map` has no length.
Source
Thrown at tpl/internal/go_templates/texttemplate/funcs.go:295
return reflect.Value{}, fmt.Errorf("invalid slice index: %d > %d", idx[0], idx[1])
}
if len(indexes) < 3 {
return item.Slice(idx[0], idx[1]), nil
}
// given item[i:j:k], make sure i <= j <= k.
if idx[1] > idx[2] {
return reflect.Value{}, fmt.Errorf("invalid slice index: %d > %d", idx[1], idx[2])
}
return item.Slice3(idx[0], idx[1], idx[2]), nil
}
// Length
// length returns the length of the item, with an error if it has no defined length.
func length(item reflect.Value) (int, error) {
item, isNil := indirect(item)
if isNil {
return 0, fmt.Errorf("len of nil pointer")
}
switch item.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return item.Len(), nil
}
return 0, fmt.Errorf("len of type %s", item.Type())
}
// Function invocation
func emptyCall(fn reflect.Value, args ...reflect.Value) reflect.Value {
panic("unreachable") // implemented as a special case in evalCall
}
// call returns the result of evaluating the first argument as a function.
// The function must return 1 result, or 2 results, the second of which is an error.
func call(name string, fn reflect.Value, args ...reflect.Value) (reflect.Value, error) {
fn = indirectInterface(fn)View on GitHub (pinned to 52c9bd7908)
Solutions
- Guard with `{{with .Ptr}}{{len .}}{{else}}0{{end}}`.
- Change the field to a value type (`[]T` instead of `*[]T`) or initialize it.
- Provide a non-nil default in the data layer.
Example fix
// before
{{len .Items}} // .Items is *[]string, nil
// after
{{with .Items}}{{len .}}{{else}}0{{end}} Defensive patterns
Strategy: validation
Validate before calling
// Guard optional pointer fields:
// {{with .Ptr}}{{len .}}{{else}}0{{end}} Type guard
func nonNilPointerOrZero(v interface{}) int {
rv := reflect.ValueOf(v)
if !rv.IsValid() { return 0 }
if rv.Kind() == reflect.Ptr && rv.IsNil() { return 0 }
if rv.Kind() == reflect.Ptr { rv = rv.Elem() }
switch rv.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return rv.Len()
}
return 0
} Prevention
- Initialize pointer fields, or change to value types.
- Wrap len in {{with}} for optional pointer fields.
- Provide non-nil defaults in the data layer.
When it happens
Trigger: `{{len .Ptr}}` where `.Ptr` is a nil pointer to a slice/string/map/chan/array; optional fields typed as pointers that were never set.
Common situations: Optional front matter or struct fields exposed as pointers; JSON unmarshaling that leaves absent fields nil; migrations from value types to pointer types.
Related errors
- slice of nil pointer
- len of type %s
- can't iterate over a nil value
- can't iterate over a nil value of type {type}
- index of nil pointer
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/270db04a4d614024.
Report an issue: GitHub.