gohugoio/hugo · error
too many slice indexes: %d
Error message
too many slice indexes: %d
What it means
Raised by the `slice` action when more than three index arguments are supplied (funcs.go:251-252). `slice` supports at most the three-index form `s[i:j:k]` (Go 3-index slice expression); four or more operands have no valid Go semantics.
Source
Thrown at tpl/internal/go_templates/texttemplate/funcs.go:252
}
// Slicing.
// slice returns the result of slicing its first argument by the remaining
// arguments. Thus "slice x 1 2" is, in Go syntax, x[1:2], while "slice x"
// is x[:], "slice x 1" is x[1:], and "slice x 1 2 3" is x[1:2:3]. The first
// argument must be a string, slice, or array.
func slice(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error) {
item = indirectInterface(item)
if !item.IsValid() {
return reflect.Value{}, fmt.Errorf("slice of untyped nil")
}
var isNil bool
if item, isNil = indirect(item); isNil {
return reflect.Value{}, fmt.Errorf("slice of nil pointer")
}
if len(indexes) > 3 {
return reflect.Value{}, fmt.Errorf("too many slice indexes: %d", len(indexes))
}
var cap int
switch item.Kind() {
case reflect.String:
if len(indexes) == 3 {
return reflect.Value{}, fmt.Errorf("cannot 3-index slice a string")
}
cap = item.Len()
case reflect.Array, reflect.Slice:
cap = item.Cap()
default:
return reflect.Value{}, fmt.Errorf("can't slice item of type %s", item.Type())
}
// set default values for cases item[:], item[i:].
idx := [3]int{0, item.Len()}
for i, index := range indexes {
x, err := indexArg(index, cap)
if err != nil {View on GitHub (pinned to 52c9bd7908)
Solutions
- Reduce to at most three indexes: `{{slice .X 1 2 3}}`.
- If you meant nested indexing use `{{index .X 1 2 3}}` instead of `slice`.
- Check the template source for stray arguments.
Example fix
// before
{{slice .S 0 2 4 6}}
// after
{{slice .S 0 2 4}} Defensive patterns
Strategy: validation
Validate before calling
// Limit slice arity in template authoring — at most 3 indexes:
// {{slice .S 0 2 4}}
// If building templates programmatically, cap indexes to len<=3. Prevention
- Remember slice mirrors Go's [i:j:k] — max 3 operands.
- Use index (not slice) for nested element access.
- Lint generated templates for slice actions with >3 args.
When it happens
Trigger: `{{slice .X 1 2 3 4}}`, or a typo/extra trailing argument; programmatic template generation that appends an extra index.
Common situations: Misunderstanding that `slice` mirrors Go's `[i:j:k]` and assumes a maximum of three operands; copy-paste errors; confused with `index` which accepts arbitrary chaining.
Related errors
- slice of untyped nil
- slice of nil pointer
- cannot 3-index slice a string
- can't slice item of type %s
- invalid slice index: %d > %d
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/6aedc6734dd26ab1.
Report an issue: GitHub.