gohugoio/hugo · error

end argument must be integer

Error message

end argument must be integer

What it means

Thrown by strings.SliceString when the end index argument cannot be converted to an int via cast.ToIntE. The end index is the second optional index; a non-integer value (float, word, nil) triggers this.

Solutions

  1. Cast the end to an integer: {{ strings.SliceString $s $start (int $end) }}.
  2. Omit the end argument if you want to slice to the end of the string.
  3. Use numeric front matter.

Example fix

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

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling {{ strings.SliceString $s 0 "abc" }}, {{ strings.SliceString $s 1 2.5 }}, or passing a non-integer end index.

Common situations: End indices sourced from string front matter; computed float values; nil params.

Related errors


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

Appendix: source

Thrown at tpl/strings/strings.go:335

// 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")
	}

	if argNum == 2 {
		if argEnd < 0 || argEnd > len(asRunes) {
			return "", errors.New("slice bounds out of range")
		}
		return string(asRunes[argStart:argEnd]), nil

View on GitHub (pinned to 52c9bd7908)