Min Heap Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Min Heap runs in O(log n) time and O(n) space, in Go as in every other language.
- ✓container/heap is not a data structure, it is an INTERFACE plus free functions. You provide a type implementing sort.Interface (Len, Less, Swap) plus Push(any) and Pop() any, and the package supplies heap.Init, heap.Push, heap.Pop and heap.Fix that drive your methods.
- ✓Implement the five methods on a slice type, then never call them directly: always go through heap.Push(h, x) and heap.Pop(h).
- ✓The Go trap: Calling h.Push(x) instead of heap.Push(&h, x) compiles perfectly and silently corrupts the heap: your method only appends, it is the package function that performs the sift.
- ✓The interface dispatch means every comparison and swap is a dynamic method call rather than an inlined one, so container/heap is measurably slower than a hand-written monomorphic heap, typically enough to matter in a hot Dijkstra loop.
What Min Heap actually is
Min Heap in Go: what you actually reach for
Go implementation
package pq
import "container/heap"
type Item struct {
Name string
Cost int
}
// A slice type implementing heap.Interface. Less decides min vs max.
type MinHeap []*Item
func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i].Cost < h[j].Cost } // min-heap
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
// POINTER receivers: these change the length. A value receiver compiles
// and silently does nothing.
func (h *MinHeap) Push(x any) { *h = append(*h, x.(*Item)) }
func (h *MinHeap) Pop() any {
old := *h
n := len(old)
item := old[n-1]
old[n-1] = nil
*h = old[:n-1]
return item
}
func Example() {
h := &MinHeap{}
heap.Init(h)
heap.Push(h, &Item{Name: "b", Cost: 5}) // heap.Push, NEVER h.Push
heap.Push(h, &Item{Name: "a", Cost: 1})
cheapest := heap.Pop(h).(*Item) // a
// decrease-key: mutate in place, then Fix. O(log n).
(*h)[0].Cost = 0
heap.Fix(h, 0)
_ = cheapest
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Min Heap?
container/heap is not a data structure, it is an INTERFACE plus free functions. You provide a type implementing sort.Interface (Len, Less, Swap) plus Push(any) and Pop() any, and the package supplies heap.Init, heap.Push, heap.Pop and heap.Fix that drive your methods. Less decides direction, so a min-heap is simply Less returning a < b.
What is the most common Go mistake when implementing Min Heap?
Calling h.Push(x) instead of heap.Push(&h, x) compiles perfectly and silently corrupts the heap: your method only appends, it is the package function that performs the sift. This is the defining container/heap bug. The second one is the receiver: Push and Pop must be on a POINTER receiver because they change the slice length, and a value receiver also compiles while doing nothing.
How does Min Heap actually perform in Go?
The interface dispatch means every comparison and swap is a dynamic method call rather than an inlined one, so container/heap is measurably slower than a hand-written monomorphic heap, typically enough to matter in a hot Dijkstra loop. Push and Pop also convert through any, which boxes non-pointer values and allocates. With Go 1.18+ generics, a hand-rolled generic heap avoids both costs. Time complexity is O(log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Devin, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.