---
source: https://iinventedlight.com/library/notes/go-index-suffixarray.md
title: "Go index/suffixarray: logarithmic substring search over in-memory data"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (The Go Authors); BSD-3-Clause (The Go Authors); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Go Authors, index/suffixarray; The Go Authors, suffixarray.go (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-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). 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-index-suffixarray"
tags: [go, suffixarray, search, index, strings]
client: Librarian
sources:
  - id: S1
    title: "index/suffixarray"
    publisher: The Go Authors
    url: https://pkg.go.dev/index/suffixarray
    accessed: 2026-11-26
    license: "BSD-3-Clause"
  - id: S2
    title: "suffixarray.go (source)"
    publisher: The Go Authors
    url: https://go.dev/src/index/suffixarray/suffixarray.go
    accessed: 2026-11-26
    license: "BSD-3-Clause"
---
# Go index/suffixarray: logarithmic substring search over in-memory data
**Answer:** Package suffixarray implements substring search in logarithmic time using an in-memory suffix array.

## Building the index

New builds an Index over data. Creation time is O(N), with N equal to len(data).

Bytes returns the data over which the index was created. It must not be modified.

Write writes the index x to w. Read reads the index from r into x; x must not be nil.

## Looking up substrings

Lookup returns indices where s occurs. When n is -1, Lookup returns all indices where s occurs. Lookup can also return a list of at most n indices.

The result is nil if s is empty, if s is not found, or if n == 0.

A Lookup costs O(log(N)*len(s) + len(result)), and N is the indexed data size.

FindAllIndex returns a sorted list of non-overlapping matches of the regular expression r. If n < 0, all matches are returned in successive order.

## See also

- [go-regexp-syntax](https://iinventedlight.com/library/notes/go-regexp-syntax/)

## Claims

- id: C1
  claim: Building the index takes time linear in the data length.
  source: S1
  evidence: "Index creation time is O(N) for N = len(data)."

- id: C2
  claim: The underlying data slice must not be modified.
  source: S1
  evidence: "It must not be modified."

- id: C3
  claim: The package comment describes logarithmic-time substring search.
  source: S2
  evidence: "Package suffixarray implements substring search in logarithmic time using"

## Sources

- [S1] index/suffixarray. The Go Authors. https://pkg.go.dev/index/suffixarray . License: BSD-3-Clause. Accessed 2026-11-26.

- [S2] suffixarray.go (source). The Go Authors. https://go.dev/src/index/suffixarray/suffixarray.go . License: BSD-3-Clause. Accessed 2026-11-26.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, index/suffixarray; The Go Authors, suffixarray.go (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).
