gohugoio/hugo · error
requires at least 2 arguments
Error message
requires at least 2 arguments
What it means
Thrown by strings.ReplacePairs when fewer than 2 arguments are supplied. ReplacePairs needs at least a replacement spec (slice or one pair) and the source string; the last arg is always the source.
Solutions
- Provide at least 2 arguments ending with the source string.
- Use the slice form: strings.ReplacePairs (slice "a" "b") $s.
- Use the inline form: strings.ReplacePairs "a" "b" $s.
- Count arguments in the template call.
Example fix
// before:
{{ strings.ReplacePairs $s }}
// after (inline pair + source):
{{ strings.ReplacePairs "a" "b" $s }} Defensive patterns
Strategy: validation
Validate before calling
{{ if ge (len $args) 2 }}{{ strings.ReplacePairs $args... }}{{ else }}{{ warnf "ReplacePairs needs >=2 args" }}{{ end }} Prevention
- Always end the call with the source string as the last argument.
- Use the slice form when pairs are dynamic.
- Count arguments before invoking.
When it happens
Trigger: Calling {{ strings.ReplacePairs }} or {{ strings.ReplacePairs $s }} with zero or one argument. Both forms (inline pairs or slice) require the source string as the last positional argument.
Common situations: Forgetting the source string, calling with no args by mistake, or assuming ReplacePairs takes a map argument as a single value.
Related errors
- uneven number of replacement pairs
- Defer does not take any arguments
- end argument must be integer
- failed to convert content to string
- failed to convert s to string
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/0eeb5a114e22f537.
Report an issue: GitHub.
Appendix: source
Thrown at tpl/strings/strings.go:265
if len(limit) == 0 {
return strings.ReplaceAll(ss, so, sn), nil
}
lim, err := cast.ToIntE(limit[0])
if err != nil {
return "", err
}
return strings.Replace(ss, so, sn, lim), nil
}
// ReplacePairs returns a copy of a string with multiple replacements performed
// in a single pass. The last argument is the source string. Preceding arguments
// are old/new string pairs, either as a slice or as individual arguments.
func (ns *Namespace) ReplacePairs(args ...any) (string, error) {
if len(args) < 2 {
return "", fmt.Errorf("requires at least 2 arguments")
}
ss, err := cast.ToStringE(args[len(args)-1])
if err != nil {
return "", err
}
var p []string
if len(args) == 2 {
// slice form: ReplacePairs (slice "a" "b") "s"
if !hreflect.IsSlice(args[0]) {
return "", fmt.Errorf("with 2 arguments, the first must be a slice of replacement pairs, got %T", args[0])
}
p, err = cast.ToStringSliceE(args[0])
if err != nil {
return "", err
}
}View on GitHub (pinned to 52c9bd7908)