{"record":{"id":"f3b4cf55971c9278","repo":"gohugoio/hugo","slug":"cannot-index-slice-array-with-type-s","errorCode":null,"errorMessage":"cannot index slice/array with type %s","messagePattern":"cannot index slice/array with type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/index.go","lineNumber":90,"sourceCode":"\t\tindex := reflect.ValueOf(i)\n\t\tvar isNil bool\n\t\tif v, isNil = hreflect.Indirect(v); isNil {\n\t\t\t// See issue 10489\n\t\t\t// This used to be an error.\n\t\t\treturn nil, nil\n\t\t}\n\t\tswitch v.Kind() {\n\t\tcase reflect.Array, reflect.Slice, reflect.String:\n\t\t\tvar x int64\n\t\t\tswitch index.Kind() {\n\t\t\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\t\t\tx = index.Int()\n\t\t\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\n\t\t\t\tx = int64(index.Uint())\n\t\t\tcase reflect.Invalid:\n\t\t\t\treturn nil, errors.New(\"cannot index slice/array with nil\")\n\t\t\tdefault:\n\t\t\t\treturn nil, fmt.Errorf(\"cannot index slice/array with type %s\", index.Type())\n\t\t\t}\n\t\t\tif x < 0 || x >= int64(v.Len()) {\n\t\t\t\t// We deviate from stdlib here.  Don't return an error if the\n\t\t\t\t// index is out of range.\n\t\t\t\treturn nil, nil\n\t\t\t}\n\t\t\tv = v.Index(int(x))\n\t\tcase reflect.Map:\n\t\t\tindex, err := prepareArg(index, v.Type().Key())\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tif x := v.MapIndex(index); x.IsValid() {\n\t\t\t\tv = x\n\t\t\t} else {\n\t\t\t\tv = reflect.Zero(v.Type().Elem())\n\t\t\t}","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/index.go#L72-L108","documentation":"Thrown inside doIndex (surfaced via the Index wrapper) when indexing an array, slice, or string with a reflect.Kind that is not an integer and not Invalid (nil has its own message). Only signed/unsigned integer kinds are accepted as positional indices; floats, strings, bools, structs, etc. are rejected with the index's reflect.Type named via %s.","triggerScenarios":"Calling `{{ index $slice \"0\" }}` (string key), `{{ index $slice 1.5 }}` (float), `{{ index $str true }}`, or `{{ index $array $someStruct }}`. The switch in doIndex over index.Kind only accepts int*/uint* families; everything non-nil falls to the default branch producing this error.","commonSituations":"Front matter supplies indices as strings (\"0\") which are not auto-coerced for slices (unlike maps), computed float indices, or template variables typed as interface{} holding a string. Frequent when data crosses JSON boundaries where numbers may arrive as strings.","solutions":["Coerce the key to an integer before indexing: `{{ index $slice (int $k) }}`.","Use integer literals directly: `{{ index $slice 2 }}`.","If the key arrives as a string from JSON, convert upstream in a content adapter or partial.","Switch to a map if string keys are genuinely needed."],"exampleFix":"// before\n{{ index $slice \"1\" }}\n// after\n{{ index $slice 1 }}","handlingStrategy":"type-guard","validationCode":"func isIntegerKind(v any) bool {\n    k := reflect.ValueOf(v).Kind()\n    switch k {\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}","typeGuard":"func isValidSliceIndex(k any) bool {\n    return isIntegerKind(k)\n}","tryCatchPattern":null,"preventionTips":["Use integer literals for slice/array/string indices.","Coerce string-encoded numbers with int() before indexing slices.","Standardize JSON/CSV indices as integers at ingestion.","Use a map if you genuinely need string keys."],"tags":["go","hugo","template","collections","index","type-error"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}