DEV.co
Open-Source Observability · sanity-io

litter

Litter is a Go library that formats data structures into readable, syntactically valid Go literals for debugging and testing. It outputs consistent, sorted snapshots useful for unit test comparisons and handles circular references automatically.

Source: GitHub — github.com/sanity-io/litter
1.7k
GitHub stars
60
Forks
Go
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorysanity-io/litter
Ownersanity-io
Primary languageGo
LicenseMIT — OSI-approved
Stars1.7k
Forks60
Open issues17
Latest releaseUnknown
Last updated2026-05-04
Sourcehttps://github.com/sanity-io/litter

What litter is

A Go pretty-printer that converts arbitrary data structures into literal Go syntax with configurable output (compact/verbose, field filtering, package name stripping). Supports custom dumper interfaces and maintains pointer aliasing for circular reference detection.

Quickstart

Get the litter source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/sanity-io/litter.gitcd litter# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Snapshot Testing in Go

Generate consistent, human-readable test fixtures by dumping test results to files. Subsequent test runs compare actual output against saved Litter snapshots, eliminating fragile JSON/YAML comparisons.

Interactive Debugging

Replace fmt.Printf debugging with Litter.Dump() calls to visualize complex nested structs, slices, and pointers in a readable format during development and troubleshooting.

Logging Complex State in Tests

When test assertions fail, use Sdump() to serialize expected vs. actual state as Go literals for immediate visual inspection without external deserialization tools.

Implementation considerations

  • Configure HidePrivateFields and FieldExclusions upfront to avoid leaking unexported struct fields or generated code artifacts in test snapshots.
  • Use local litter.Options for test-specific configurations rather than modifying global Config to prevent test interdependencies.
  • Implement the Dumper interface on custom types to control serialization format and avoid verbose or irrelevant field output.
  • Plan snapshot file management: decide whether to commit snapshot files to version control and how to handle intentional data structure changes.
  • Test circular reference handling explicitly if your domain model contains self-referencing pointers to ensure alias output is stable.

When to avoid it — and what to weigh

  • Production Performance Logging — Litter is optimized for readability and consistency, not speed. Avoid using it in hot paths or high-throughput logging scenarios where structured JSON/protobuf serialization is required.
  • Cross-Language Serialization — Output is syntactically correct Go only; it cannot be parsed by other languages. For APIs or multi-service systems, use standard formats (JSON, protobuf) instead.
  • Handling Very Large Data Structures — Litter holds the entire output in memory (Sdump) or writes synchronously. Extremely large object graphs may consume significant resources; stream-based serialization is a better choice.
  • Security-Sensitive Data Output — Litter will dump all visible fields and values. No built-in redaction; sensitive data (passwords, tokens) will appear in plaintext in output if not manually filtered first.

License & commercial use

Licensed under MIT. This is a permissive open-source license permitting commercial use, modification, and distribution with attribution.

MIT license permits unrestricted commercial use. No restrictions on proprietary software integrations. Attribution appreciated but not legally required. No warranty provided; consult internal legal review if including in critical systems.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

Litter performs reflection-based introspection on input data and outputs all visible fields and values. Ensure sensitive data (credentials, keys, PII) is not passed to Litter or is explicitly filtered beforehand using FieldExclusions. No input sanitization or escaping guarantees; output is Go syntax only.

Alternatives to consider

Spew (davecgh/go-spew)

Predecessor project; similar reflection-based pretty-printing. Litter claims to focus on terseness and readability; evaluate if Spew's more verbose output is acceptable for your use case.

encoding/json with MarshalIndent

Standard library alternative for snapshot testing. Output is portable across languages and parseable; less readable for Go-specific types (pointers, unexported fields, nil).

Custom fmt.Sprintf or String() methods

Fine-grained control over output format. More verbose to implement; useful if Litter's defaults do not suit domain models or you need specialized serialization logic.

Software development agency

Build on litter with DEV.co software developers

If your Go team needs readable snapshot testing or better debug output, Litter is a low-friction library to adopt. Review the configuration options and custom Dumper interface to fit your testing practices. MIT license allows immediate commercial use.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

litter FAQ

Can I use Litter output in production code?
Not recommended. Litter is designed for debugging and testing. For production serialization, use json, encoding/gob, or protobuf. Output is not guaranteed stable across versions.
Does Litter support custom types (e.g., time.Time, *sql.NullInt64)?
Yes, via the Dumper interface. Implement LitterDump(io.Writer) on your type to customize its output. Standard library types are handled with sensible defaults.
How do I hide sensitive fields from Litter output?
Use FieldExclusions regexp to skip fields by name, HidePrivateFields to exclude unexported fields, or implement a custom Dumper on sensitive types.
Is Litter performance-critical code or just for testing?
Optimized for readability and consistency, not speed. Suitable for test suites and debugging; avoid in hot paths or high-frequency logging.

Work with a software development agency

DEV.co is a software development agency delivering custom software development services to companies building on open source. Our software developers and web developers design, integrate, and ship production systems — spanning web development, APIs, AI, data, and cloud. If litter is part of your open-source observability roadmap, our team can implement, customize, migrate, and maintain it.

Evaluate Litter for Your Go Testing Pipeline

If your Go team needs readable snapshot testing or better debug output, Litter is a low-friction library to adopt. Review the configuration options and custom Dumper interface to fit your testing practices. MIT license allows immediate commercial use.