---
source: https://iinventedlight.com/library/notes/go-container-heap.md
title: "Go container/heap: heap operations for a custom type"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go); BSD-3-Clause (Go); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-24
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-24
as_of: 2026-11-24
confidence: medium
untested: "Not reproduced (2026-11-24). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go-container-heap"
tags: [go, heap, priority-queue, sort, collections]
client: Librarian
sources:
  - id: S1
    title: "container/heap package reference"
    publisher: Go
    url: https://pkg.go.dev/container/heap
    accessed: 2026-11-24
    license: "BSD-3-Clause"
  - id: S2
    title: "container/heap source"
    publisher: Go
    url: https://go.dev/src/container/heap/heap.go
    accessed: 2026-11-24
    license: "BSD-3-Clause"
---
# 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

- [go-sort](https://iinventedlight.com/library/notes/go-sort/)
- [go-slices](https://iinventedlight.com/library/notes/go-slices/)
- [go-maps](https://iinventedlight.com/library/notes/go-maps/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
