Go container/heap: heap operations for a custom type
Answer: Heap operations in this package work with any type that implements Interface.
The heap property
In this tree, every node is the minimum-valued node of its subtree.
The minimum element in the tree is the root, at index 0.
Priority queues are a common use of a heap.
Push adds items. Pop takes the highest-priority item off the queue.
The interface
Init sets up the heap invariants.
After an element at an index changes, Fix puts the heap back in order.
Remove removes and returns the element at index i from the heap.
See also
Claims
- id: C1 claim: The package works with any type that implements Interface. source: S1 evidence: "heap operations for any type that implements heap.Interface"
- id: C2 claim: The minimum element is the root, at index 0. source: S1 evidence: "The minimum element in the tree is the root, at index 0"
- id: C3 claim: A heap is often used to implement a priority queue. source: S2 evidence: "A heap is a common way to implement a priority queue"
- id: C4 claim: A heap is a tree of minimum-valued nodes in each subtree. source: S2 evidence: "minimum-valued node"
Sources
- [S1] container/heap package reference. Go. https://pkg.go.dev/container/heap . License: BSD-3-Clause. Accessed 2026-11-24.
- [S2] container/heap source. Go. https://go.dev/src/container/heap/heap.go . License: BSD-3-Clause. Accessed 2026-11-24.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go, container/heap package reference; Go, container/heap source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go, container/heap package reference; Go, container/heap source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go container/heap: heap operations for a custom type." iinventedlight Library, iil:note:d26x7dwn, version of 2026-11-24. https://iinventedlight.com/library/notes/go-container-heap/. License: CC-BY-4.0.
@misc{iil_note_d26x7dwn,
author = {{iinventedlight}},
title = {{Go container/heap: heap operations for a custom type}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-24},
url = {https://iinventedlight.com/library/notes/go-container-heap/},
note = {Permanent ID iil:note:d26x7dwn. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-container-heap.schema.json (schema; CC-BY-4.0)
- go-container-heap.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-container-heap.md.