{"record":{"id":"ef1982d95be8b785","repo":"gohugoio/hugo","slug":"cannot-index-slice-array-with-nil-ef1982","errorCode":null,"errorMessage":"cannot index slice/array with nil","messagePattern":"cannot index slice/array with nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/texttemplate/funcs.go","lineNumber":183,"sourceCode":"\tswitch typ {\n\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\treturn true\n\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\n\t\treturn true\n\t}\n\treturn false\n}\n\n// indexArg checks if a reflect.Value can be used as an index, and converts it to int if possible.\nfunc indexArg(index reflect.Value, cap int) (int, error) {\n\tvar x int64\n\tswitch index.Kind() {\n\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\tx = index.Int()\n\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\n\t\tx = int64(index.Uint())\n\tcase reflect.Invalid:\n\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\")","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/texttemplate/funcs.go#L165-L201","documentation":"Returned by indexArg when the index value provided to index a slice/array/string is reflect-invalid (untyped nil, e.g. an unset variable or an explicit nil). Indexing requires a concrete integer; nil is not coercible. This backs the index builtin and slice bounds checking.","triggerScenarios":"Using {{index .Slice $i}} where $i is an unset variable or nil; {{index .Arr nil}}; a pipeline that produces no value being used as the index.","commonSituations":"A range variable not bound in an edge case; optional query params that are nil passed straight to index; refactoring that left a variable uninitialized.","solutions":["Ensure the index expression yields a concrete int before calling index.","Guard with {{if $i}}{{index .Slice $i}}{{end}} or default to 0: {{$i := or $i 0}}.","Validate index is non-nil and numeric in the data layer before rendering.","Provide explicit defaults for optional values feeding index."],"exampleFix":"// before\n{{index .Items .Selected}} // .Selected is nil -> error\n\n// after\n{{$idx := or .Selected 0}}\n{{index .Items $idx}}","handlingStrategy":"validation","validationCode":"// register a nil-safe index helper\ntemplate.Funcs(template.FuncMap{\n    \"at\": func(seq any, i any) (any, error) {\n        if i == nil { return nil, fmt.Errorf(\"index is nil\") }\n        return index(reflect.ValueOf(seq), reflect.ValueOf(i))\n    },\n})","typeGuard":"func isIntegerIndex(i any) bool {\n    switch reflect.TypeOf(i).Kind() {\n    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,\n         reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Default optional index values: {{$i := or .Selected 0}}.","Register a nil-safe index helper used by templates.","Validate indices are non-nil integers in the data layer."],"tags":["go","text-template","index","reflect","nil"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}