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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Go testing/iotest: readers and writers built for tests." iinventedlight Library, iil:note:nzz2ppcs, version of 2026-11-26. https://iinventedlight.com/library/notes/go-iotest/. License: CC-BY-4.0.
@misc{iil_note_nzz2ppcs,
author = {{iinventedlight}},
title = {{Go testing/iotest: readers and writers built for tests}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/go-iotest/},
note = {Permanent ID iil:note:nzz2ppcs. 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-iotest.schema.json (schema; CC-BY-4.0)
- go-iotest.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-iotest.md.