---
source: https://iinventedlight.com/library/notes/go-iotest.md
title: "Go testing/iotest: readers and writers built for tests"
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, testing/iotest; The Go Authors, reader.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-iotest"
tags: [go, iotest, testing, io, readers]
client: Librarian
sources:
  - id: S1
    title: "testing/iotest"
    publisher: The Go Authors
    url: https://pkg.go.dev/testing/iotest
    accessed: 2026-11-26
    license: "BSD-3-Clause"
  - id: S2
    title: "reader.go (source)"
    publisher: The Go Authors
    url: https://go.dev/src/testing/iotest/reader.go
    accessed: 2026-11-26
    license: "BSD-3-Clause"
---
# Go testing/iotest: readers and writers built for tests
**Answer:** Package iotest implements Readers and Writers useful mainly for testing.

## Readers that change how Read behaves

ErrReader returns an io.Reader that returns 0, err from all Read calls.

HalfReader implements Read by taking only half as many requested bytes from r.

DataErrReader changes the way errors are handled by a Reader.

A typical Reader returns an error, often EOF, on the first Read after the last piece of data.

This wrapper returns the final error along with the final data, instead of on the first call after the final data.

ErrTimeout is a fake timeout error. The package documentation lists OneByteReader, TimeoutReader, TestReader, and TruncateWriter.

## Loggers for tests

NewReadLogger wraps r and logs each read to standard error with log.Printf, including a prefix and hexadecimal data.

NewWriteLogger wraps w and logs each write to standard error with log.Printf, including a prefix and hexadecimal data.

## See also

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

## Claims

- id: C1
  claim: The package is aimed mainly at tests.
  source: S1
  evidence: "Package iotest implements Readers and Writers useful mainly for testing."

- id: C2
  claim: ErrReader always returns zero bytes and the given error.
  source: S1
  evidence: "ErrReader returns an io.Reader that returns 0, err from all Read calls."

- id: C3
  claim: DataErrReader changes when a Reader reports an error.
  source: S2
  evidence: "DataErrReader changes the way errors are handled by a Reader."

## Sources

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

- [S2] reader.go (source). The Go Authors. https://go.dev/src/testing/iotest/reader.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, testing/iotest; The Go Authors, reader.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).
