gohugoio/hugo · error

len of type %s

Error message

len of type %s

What it means

Raised by the `len` template function when the argument's reflect.Kind is not Array, Chan, Map, Slice, or String (funcs.go:297-301). `len` only works on types with an intrinsic length; structs, ints, bools, funcs, and pointers-to-struct fall through to the error. The %s is the value's Go type.

Source

Thrown at tpl/internal/go_templates/texttemplate/funcs.go:301

	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)
	if !fn.IsValid() {
		return reflect.Value{}, fmt.Errorf("call of nil")
	}
	typ := fn.Type()
	if typ.Kind() != reflect.Func {
		return reflect.Value{}, fmt.Errorf("non-function %s of type %s", name, typ)

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Print the type: `{{printf "%T" .X}}`.
  2. Use the appropriate accessor (struct fields, range for maps, etc.).
  3. Change the data so the value is one of the supported length types.

Example fix

// before
{{len .Config}}   // .Config is a struct

// after
{{len (reflect.Map .Config)}}   // or expose a slice field in Go
Defensive patterns

Strategy: type-guard

Validate before calling

// Confirm the value has a length kind before calling len:
//   {{printf "%T" .X}}
// Only len Array/Chan/Map/Slice/String.

Type guard

func hasLen(v interface{}) bool {
    if v == nil { return false }
    switch reflect.TypeOf(v).Kind() {
    case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
        return true
    }
    return false
}

Prevention

When it happens

Trigger: `{{len .Int}}`, `{{len .Struct}}`, `{{len .Bool}}`, `{{len .Func}}`.

Common situations: Expecting `len` on a struct to count fields (it does not); calling `len` on a number; refactoring a field from a slice to a scalar.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/df546aa0d458c433. Report an issue: GitHub.