{"record":{"id":"853c0d705e9e8b27","repo":"gohugoio/hugo","slug":"invalid-type-for-comparison","errorCode":null,"errorMessage":"invalid type for comparison","messagePattern":"invalid type for comparison","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/texttemplate/funcs.go","lineNumber":402,"sourceCode":"}\n\n// or computes the Boolean OR of its arguments, returning\n// the first true argument it encounters, or the last argument.\nfunc or(arg0 reflect.Value, args ...reflect.Value) reflect.Value {\n\tpanic(\"unreachable\") // implemented as a special case in evalCall\n}\n\n// not returns the Boolean negation of its argument.\nfunc not(arg reflect.Value) bool {\n\treturn !truth(arg)\n}\n\n// Comparison.\n\n// TODO: Perhaps allow comparison between signed and unsigned integers.\n\nvar (\n\terrBadComparisonType = errors.New(\"invalid type for comparison\")\n\terrNoComparison      = errors.New(\"missing argument for comparison\")\n)\n\ntype kind int\n\nconst (\n\tinvalidKind kind = iota\n\tboolKind\n\tcomplexKind\n\tintKind\n\tfloatKind\n\tstringKind\n\tuintKind\n)\n\nfunc basicKind(v reflect.Value) (kind, error) {\n\tswitch v.Kind() {\n\tcase reflect.Bool:","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/texttemplate/funcs.go#L384-L420","documentation":"Defined as errBadComparisonType in tpl/internal/go_templates/texttemplate/funcs.go:402 and returned by the template comparison operators (eq, ne, lt, le, gt, ge). It fires when basicKind() cannot classify an operand (e.g. a struct, map, slice, or func) or when lt/le/gt/ge is applied to bool or complex values, which are not orderable. This is the underlying text/template engine reused by Hugo.","triggerScenarios":"Writing {{ if lt .Page .OtherPage }} (comparing structs), {{ if lt $map1 $map2 }} (comparing maps), {{ if eq $slice1 $slice2 }} with non-comparable slices, or {{ if lt .Flag true }} (ordering booleans). Also comparing a struct field that is itself a non-basic type.","commonSituations":"Comparing page objects instead of a comparable field like .RelPermalink or .Date; assuming eq works on arbitrary structs; ordering booleans; comparing complex128 values.","solutions":["Compare a comparable field: use .Date, .Weight, .RelPermalink, .Title, etc. instead of the whole object.","For ordering, ensure both operands are bool/int/uint/float/string and the same kind (int vs uint has a special case, others must match).","For equality on structs/maps, compare a unique key field or use a custom approach (e.g. .File.Path).","Do not use lt/le/gt/ge on bool or complex values; convert booleans to ints if you need ordering."],"exampleFix":"// before\n{{ if lt .Page .OtherPage }}\n\n// after\n{{ if lt .Date .OtherPage.Date }}","handlingStrategy":"validation","validationCode":"// Compare a comparable field rather than whole objects:\n{{ if lt .Date .OtherPage.Date }}...{{ end }}\n// Avoid lt/le/gt/ge on bool or complex; for equality on primitives only.","typeGuard":"// Go caller: only bool/int/uint/float/string/complex are comparable via basicKind.\nfunc orderable(k reflect.Kind) bool {\n    switch k {\n    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,\n        reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,\n        reflect.Float32, reflect.Float64, reflect.String:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Never pass structs, maps, slices, or funcs to eq/lt/le/gt/ge.","For ordering, restrict to int/uint/float/string of matching kinds.","Compare a representative field (.Date, .Weight, .RelPermalink) instead of the whole object.","Do not use lt/le/gt/ge on bool or complex values."],"tags":["hugo","go-template","text-template","type-validation","comparison"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}