{"id":"96de9876990450ea","repo":"gohugoio/hugo","slug":"modulo-operator-can-t-be-used-with-non-integer-val","errorCode":null,"errorMessage":"modulo operator can't be used with non integer value","messagePattern":"modulo operator can't be used with non integer value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/math/math.go","lineNumber":166,"sourceCode":"}\n\n// MaxInt64 returns the maximum value for a signed 64-bit integer.\nfunc (ns *Namespace) MaxInt64() int64 {\n\treturn math.MaxInt64\n}\n\n// Min returns the smaller of all numbers in inputs. Any slices in inputs are flattened.\nfunc (ns *Namespace) Min(inputs ...any) (minimum float64, err error) {\n\treturn ns.applyOpToScalarsOrSlices(\"Min\", math.Min, inputs...)\n}\n\n// Mod returns n1 % n2.\nfunc (ns *Namespace) Mod(n1, n2 any) (int64, error) {\n\tai, erra := cast.ToInt64E(n1)\n\tbi, errb := cast.ToInt64E(n2)\n\n\tif erra != nil || errb != nil {\n\t\treturn 0, errors.New(\"modulo operator can't be used with non integer value\")\n\t}\n\n\tif bi == 0 {\n\t\treturn 0, errors.New(\"the number can't be divided by zero at modulo operation\")\n\t}\n\n\treturn ai % bi, nil\n}\n\n// ModBool returns the boolean of n1 % n2.  If n1 % n2 == 0, return true.\nfunc (ns *Namespace) ModBool(n1, n2 any) (bool, error) {\n\tres, err := ns.Mod(n1, n2)\n\tif err != nil {\n\t\treturn false, err\n\t}\n\n\treturn res == int64(0), nil\n}","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/math/math.go#L148-L184","documentation":"Hugo's `math.Mod` template function (Namespace.Mod in tpl/math/math.go) casts both operands to int64 via `cast.ToInt64E` before computing `n1 % n2`. If either cast fails — because the value is a non-numeric string, a nil, a map/slice, or otherwise not integer-convertible — Hugo returns this error instead of guessing. `ModBool` delegates to `Mod`, so it surfaces the same error.","triggerScenarios":"Calling `{{ mod A B }}` or `{{ modBool A B }}` in a Hugo template where A or B is not convertible to int64: e.g. `{{ mod \"abc\" 3 }}`, `{{ mod .Params.count 2 }}` when the front-matter param is missing (nil) or a string like \"10px\", or passing a float string that cast.ToInt64E rejects. Note spf13/cast truncates plain floats, so the usual trigger is strings/nil, not floats.","commonSituations":"Alternating row striping with `{{ if modBool $index 2 }}` where the index variable is accidentally a string from a data file (JSON/YAML numbers quoted); front-matter params typed as strings; `.Params` lookups that return nil because the key is absent or cased differently; values piped from `printf` or `.Get` shortcode params which are always strings.","solutions":["Ensure both operands are numeric: convert with `{{ mod (int $a) (int $b) }}` when the value may be a string.","Guard against missing params: `{{ with .Params.count }}{{ mod (int .) 2 }}{{ end }}` or provide a default: `{{ mod (int (default 0 .Params.count)) 2 }}`.","Fix the data source so numbers are unquoted in JSON/YAML/TOML front matter.","When using shortcode params (`.Get`), remember they are strings — always wrap in `int`."],"exampleFix":"<!-- before -->\n{{ if modBool .Params.rank 2 }}even{{ end }}\n<!-- after -->\n{{ if modBool (int (default 0 .Params.rank)) 2 }}even{{ end }}","handlingStrategy":"type-guard","validationCode":"{{ if and (eq (printf \"%T\" $a) \"int\") (eq (printf \"%T\" $b) \"int\") }}{{ mod $a $b }}{{ end }}","typeGuard":"// Go caller side\nfunc isInt(v any) bool {\n    switch v.(type) {\n    case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:\n        return true\n    }\n    return false\n}","tryCatchPattern":"if _, err := ns.ModBool(a, b); err != nil {\n    // fall back or surface template error\n}","preventionTips":["Cast operands with `int` before calling `mod` in templates: `{{ mod (int $a) (int $b) }}`","Remember front matter numbers parse as float64 in YAML/TOML — convert explicitly","Never pass strings from .Params directly into math functions"],"tags":["hugo","go-templates","math","type-conversion"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}