Implement the interface
type IntHeap []int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}
h := &IntHeap{3, 1, 4, 1, 5}
heap.Init(h)
heap.Push(h, 9)
for h.Len() > 0 {
fmt.Printf("%d ", heap.Pop(h))
}
Output
1 1 3 4 5 9