{"record":{"id":"7dbcb6dbfc3ac316","repo":"gohugoio/hugo","slug":"cannot-index-slice-array-with-type-s-7dbcb6","errorCode":null,"errorMessage":"cannot index slice/array with type %s","messagePattern":"cannot index slice/array with type (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/texttemplate/funcs.go","lineNumber":185,"sourceCode":"\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\")\n\t}\n\tfor _, index := range indexes {","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/texttemplate/funcs.go#L167-L203","documentation":"Returned by indexArg when the index value is valid but not an integer or unsigned-integer kind (e.g. a string, struct, float, bool). Slice/array/string indexing requires an integer index; non-integer types are rejected. The %s is the index value's type.","triggerScenarios":"Calling {{index .Slice .Name}} where .Name is a string; passing a float or struct as an index; indexing with a map-derived key of the wrong kind.","commonSituations":"URL params arriving as strings used directly as indices; a field that holds an identifier string mistaken for a numeric position; mixing map-style and slice-style indexing.","solutions":["Convert the index to an integer in the template via a helper func before indexing.","Prepare indices as ints in the data layer (strconv.Atoi) before render.","Use map access ({{.Map.key}}) instead of index when the key is non-numeric.","Add a typed accessor func that does the conversion safely."],"exampleFix":"// before\n{{index .Items .Pos}} // .Pos is string \"3\"\n\n// after\n// register \"atoi\": func(s string) (int, error)\n{{$i := atoi .Pos}}\n{{index .Items $i}}","handlingStrategy":"type-guard","validationCode":"// register \"atoi\" and use it before indexing\ntemplate.Funcs(template.FuncMap{\n    \"atoi\": func(s string) (int, error) { return strconv.Atoi(s) },\n})\n// template: {{$i := atoi .Pos}}{{index .Items $i}}","typeGuard":"func isNumericIndex(i any) bool {\n    if i == nil { return false }\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:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Convert string params to int (strconv.Atoi) before indexing.","Use map access for non-numeric keys instead of index.","Keep collection indexing strictly integer-typed in the data model."],"tags":["go","text-template","index","reflect","type-mismatch"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}