{"record":{"id":"33868173260fe7e4","repo":"TheAlgorithms/Go","slug":"less-func-is-necessary","errorCode":null,"errorMessage":"less func is necessary","messagePattern":"less func is necessary","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/heap/heap.go","lineNumber":26,"sourceCode":"// Heap heap implementation using generic.\ntype Heap[T any] struct {\n\theaps    []T\n\tlessFunc func(a, b T) bool\n}\n\n// New gives a new heap object.\nfunc New[T constraints.Ordered]() *Heap[T] {\n\tless := func(a, b T) bool {\n\t\treturn a < b\n\t}\n\th, _ := NewAny[T](less)\n\treturn h\n}\n\n// NewAny gives a new heap object. element can be anything, but must provide less function.\nfunc NewAny[T any](less func(a, b T) bool) (*Heap[T], error) {\n\tif less == nil {\n\t\treturn nil, errors.New(\"less func is necessary\")\n\t}\n\treturn &Heap[T]{\n\t\tlessFunc: less,\n\t}, nil\n}\n\n// Push pushes the element t onto the heap.\n// The complexity is O(log n) where n = h.Len().\nfunc (h *Heap[T]) Push(t T) {\n\th.heaps = append(h.heaps, t)\n\th.up(len(h.heaps) - 1)\n}\n\n// Top returns the minimum element (according to Less) from the heap.\n// Top panics if the heap is empty.\nfunc (h *Heap[T]) Top() T {\n\treturn h.heaps[0]\n}","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/heap/heap.go#L8-L44","documentation":"NewAny builds a generic Heap that delegates all element ordering to a caller-supplied less function. Because a nil lessFunc would cause a nil-function panic on the first comparison, the constructor validates it up front and returns this error.","triggerScenarios":"Calling NewAny[T](nil), most often when the comparison function is stored in a variable that was never assigned, or when passing a method value that resolved to nil.","commonSituations":"Conditionally chosen comparators left nil on some code path; refactoring removed the closure but kept the call; generic helper functions that accept a func parameter which callers forget to supply.","solutions":["Pass a valid func(a, b T) bool comparison to NewAny","Check the error returned by NewAny before using the heap","Provide a default comparator when the caller's is nil"],"exampleFix":"// before\nh, _ := heap.NewAny[int](nil) // panics later on Push\n// after\nless := func(a, b int) bool { return a < b }\nh, err := heap.NewAny[int](less)\nif err != nil {\n    return err\n}","handlingStrategy":"validation","validationCode":"if less == nil {\n    return errors.New(\"heap: comparator must not be nil\")\n}\nh, err := heap.NewAny[T](less)","typeGuard":"func hasComparator[T any](f func(a, b T) bool) bool { return f != nil }","tryCatchPattern":"h, err := heap.NewAny[T](less)\nif err != nil {\n    return nil, fmt.Errorf(\"heap init: %w\", err)\n}","preventionTips":["Always check the constructor's error before using the heap","Provide a default comparator fallback in wrapper helpers","Keep comparators next to heap construction so they are not dropped in refactors"],"tags":["go","data-structures","heap","nil-check","constructor-validation"],"backgroundTag":"nil-callback-argument","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}