{"record":{"id":"df546aa0d458c433","repo":"gohugoio/hugo","slug":"len-of-type-s","errorCode":null,"errorMessage":"len of type %s","messagePattern":"len of type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/texttemplate/funcs.go","lineNumber":301,"sourceCode":"\tif idx[1] > idx[2] {\n\t\treturn reflect.Value{}, fmt.Errorf(\"invalid slice index: %d > %d\", idx[1], idx[2])\n\t}\n\treturn item.Slice3(idx[0], idx[1], idx[2]), nil\n}\n\n// Length\n\n// length returns the length of the item, with an error if it has no defined length.\nfunc length(item reflect.Value) (int, error) {\n\titem, isNil := indirect(item)\n\tif isNil {\n\t\treturn 0, fmt.Errorf(\"len of nil pointer\")\n\t}\n\tswitch item.Kind() {\n\tcase reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:\n\t\treturn item.Len(), nil\n\t}\n\treturn 0, fmt.Errorf(\"len of type %s\", item.Type())\n}\n\n// Function invocation\n\nfunc emptyCall(fn reflect.Value, args ...reflect.Value) reflect.Value {\n\tpanic(\"unreachable\") // implemented as a special case in evalCall\n}\n\n// call returns the result of evaluating the first argument as a function.\n// The function must return 1 result, or 2 results, the second of which is an error.\nfunc call(name string, fn reflect.Value, args ...reflect.Value) (reflect.Value, error) {\n\tfn = indirectInterface(fn)\n\tif !fn.IsValid() {\n\t\treturn reflect.Value{}, fmt.Errorf(\"call of nil\")\n\t}\n\ttyp := fn.Type()\n\tif typ.Kind() != reflect.Func {\n\t\treturn reflect.Value{}, fmt.Errorf(\"non-function %s of type %s\", name, typ)","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/texttemplate/funcs.go#L283-L319","documentation":"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.","triggerScenarios":"`{{len .Int}}`, `{{len .Struct}}`, `{{len .Bool}}`, `{{len .Func}}`.","commonSituations":"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.","solutions":["Print the type: `{{printf \"%T\" .X}}`.","Use the appropriate accessor (struct fields, range for maps, etc.).","Change the data so the value is one of the supported length types."],"exampleFix":"// before\n{{len .Config}}   // .Config is a struct\n\n// after\n{{len (reflect.Map .Config)}}   // or expose a slice field in Go","handlingStrategy":"type-guard","validationCode":"// Confirm the value has a length kind before calling len:\n//   {{printf \"%T\" .X}}\n// Only len Array/Chan/Map/Slice/String.","typeGuard":"func hasLen(v interface{}) bool {\n    if v == nil { return false }\n    switch reflect.TypeOf(v).Kind() {\n    case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Don't expect len on structs/ints/bools.","Use {{printf \"%T\"}} to verify type when len misbehaves.","Expose helper accessors for non-length types."],"tags":["template","len","reflection","text-template"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}