{"record":{"id":"f1629cf06a2ea2fe","repo":"gohugoio/hugo","slug":"index-of-untyped-nil","errorCode":null,"errorMessage":"index of untyped nil","messagePattern":"index of untyped nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/texttemplate/funcs.go","lineNumber":201,"sourceCode":"\t\treturn 0, fmt.Errorf(\"cannot index slice/array with nil\")\n\tdefault:\n\t\treturn 0, fmt.Errorf(\"cannot index slice/array with type %s\", index.Type())\n\t}\n\tif x < 0 || int(x) < 0 || int(x) > cap {\n\t\treturn 0, fmt.Errorf(\"index out of range: %d\", x)\n\t}\n\treturn int(x), nil\n}\n\n// Indexing.\n\n// index returns the result of indexing its first argument by the following\n// arguments. Thus \"index x 1 2 3\" is, in Go syntax, x[1][2][3]. Each\n// indexed item must be a map, slice, or array.\nfunc index(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error) {\n\titem = indirectInterface(item)\n\tif !item.IsValid() {\n\t\treturn reflect.Value{}, fmt.Errorf(\"index of untyped nil\")\n\t}\n\tfor _, index := range indexes {\n\t\tindex = indirectInterface(index)\n\t\tvar isNil bool\n\t\tif item, isNil = indirect(item); isNil {\n\t\t\treturn reflect.Value{}, fmt.Errorf(\"index of nil pointer\")\n\t\t}\n\t\tswitch item.Kind() {\n\t\tcase reflect.Array, reflect.Slice, reflect.String:\n\t\t\tx, err := indexArg(index, item.Len())\n\t\t\tif err != nil {\n\t\t\t\treturn reflect.Value{}, err\n\t\t\t}\n\t\t\titem = item.Index(x)\n\t\tcase reflect.Map:\n\t\t\tindex, err := prepareArg(index, item.Type().Key())\n\t\t\tif err != nil {\n\t\t\t\treturn reflect.Value{}, err","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/texttemplate/funcs.go#L183-L219","documentation":"Returned by the index builtin when the very first item to be indexed is reflect-invalid (untyped nil), before any indexing happens. This means you tried to index something that has no concrete type at all, e.g. {{index nil 0}} or indexing an unset/absent variable.","triggerScenarios":"Calling {{index .MaybeNil 0}} where .MaybeNil is a nil interface (no concrete value); indexing a variable that was never assigned; indexing the result of a pipeline that produced nothing.","commonSituations":"Optional context field that is nil and used directly with index; a partial invoked with nil data then indexed; conditional data not populated in some branches.","solutions":["Guard the index call: {{if .Items}}{{index .Items 0}}{{end}}.","Ensure the collection is non-nil before render (initialize empty slices/maps in the data).","Use with for nil-safe scoping: {{with .Items}}{{index . 0}}{{end}}.","Provide defaults in the data layer so the value is never an untyped nil."],"exampleFix":"// before\n{{index .List 0}} // .List is nil interface -> error\n\n// after\n{{with .List}}{{index . 0}}{{end}}\n// or ensure .List is []any{} in data","handlingStrategy":"validation","validationCode":"// ensure collections are non-nil in the data layer\ntype PageData struct{ Items []any }\nfunc sanitize(d *PageData) {\n    if d.Items == nil { d.Items = []any{} }\n}","typeGuard":"func isIndexable(v any) bool {\n    if v == nil { return false }\n    switch reflect.TypeOf(v).Kind() {\n    case reflect.Slice, reflect.Array, reflect.Map, reflect.String:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Use {{with .Items}}{{index . 0}}{{end}} for nil-safe scoping.","Initialize collections to empty (non-nil) in the data model.","Guard with {{if .Items}} before indexing."],"tags":["go","text-template","index","nil","reflect"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}