← All notes

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

Sources

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

Permanent ID: iil:note:nzz2ppcs

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.},
}

Markdown

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.

All artifact files (JSON index)

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.