gohugoio/hugo · error

start argument must be integer

Error message

start argument must be integer

What it means

Thrown by strings.SliceString when the start index argument cannot be converted to an int via cast.ToIntE. SliceString takes a string and up to two integer indices; a non-integer start (a float, a word, nil) triggers this.

Solutions

  1. Cast the start to an integer: {{ strings.SliceString $s (int $start) }}.
  2. Default the start: {{ strings.SliceString $s ($start | default 0) }}.
  3. Use numeric front matter for index fields.

Example fix

// before
{{ strings.SliceString $s .Params.start }}
// after
{{ strings.SliceString $s (int .Params.start) }}
Defensive patterns

Strategy: validation

Validate before calling

{{ strings.SliceString $s (int $start) }}

Prevention

When it happens

Trigger: Calling {{ strings.SliceString $s "abc" }}, {{ strings.SliceString $s 1.5 }}, or {{ strings.SliceString $s $nil }} where the start is non-integer.

Common situations: Index values coming from front matter as strings; float indices from computed values; nil params used as indices.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/b97cbb66ca8bb2d5. Report an issue: GitHub.

Appendix: source

Thrown at tpl/strings/strings.go:330

	return replacer.Replace(ss), nil
}

// SliceString slices a string by specifying a half-open range with
// two indices, start and end. 1 and 4 creates a slice including elements 1 through 3.
// The end index can be omitted, it defaults to the string's length.
func (ns *Namespace) SliceString(a any, startEnd ...any) (string, error) {
	aStr, err := cast.ToStringE(a)
	if err != nil {
		return "", err
	}

	var argStart, argEnd int

	argNum := len(startEnd)

	if argNum > 0 {
		if argStart, err = cast.ToIntE(startEnd[0]); err != nil {
			return "", errors.New("start argument must be integer")
		}
	}
	if argNum > 1 {
		if argEnd, err = cast.ToIntE(startEnd[1]); err != nil {
			return "", errors.New("end argument must be integer")
		}
	}

	if argNum > 2 {
		return "", errors.New("too many arguments")
	}

	asRunes := []rune(aStr)

	if argNum > 0 && (argStart < 0 || argStart >= len(asRunes)) {
		return "", errors.New("slice bounds out of range")
	}

View on GitHub (pinned to 52c9bd7908)