`package main
import (
"container/heap"
"fmt"
"math/rand"
)
type MyHeap []Member
type Member struct {
score int
name rune
}
func (h MyHeap) Len() int {
return len(h)
}
// Smallest heap
func (h MyHeap) Less(i, j int) bool {
return h[i].score < h[j].score
}
func (h MyHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *MyHeap) Pop() interface{} {
old := *h
n := len(old)
lastElement := old[n-1]
*h = old[0 : n-1]
return lastElement
}
func (h *MyHeap) Push(x interface{}) {
h = append(h, x.(Member))
}
func main() {
var m Member
h := &MyHeap{}
for i := 0; i < 10; i++ {
m.name = 'a' + rune(i)
m.score = rand.Intn(100)
h = append(h, m)
}
heap.Init(h)
for h.Len() > 0 {
fmt.Printf("%+v ", heap.Pop(h))
}
}
`