Example: io.ReadAll
io.ReadAll reads from an io.Reader until EOF and returns all successfully read bytes as a []byte.
Use it when the input is bounded and small enough to keep in memory.
Typical examples include configuration files, small HTTP responses, JSON payloads, and test data.
Quick Example
io.ReadAll does not return until the reader reaches EOF or an error occurs.
The returned []byte contains the entire input, so memory usage grows with the amount of data read.
Common Use
Read a small, bounded file into memory:
This is a reasonable choice for configuration files, certificates, templates, and other resources whose size is known and limited.
For a small config.json, keeping the entire file in memory is usually simpler than introducing streaming logic.
HTTP Response Bodies
For HTTP responses, limit the input when the response size is not fully trusted:
io.LimitReader caps the amount of data passed to io.ReadAll, preventing an unexpectedly large response from being fully buffered in memory.
Common Mistakes
1. Using io.ReadAll for large or unbounded input
This loads the entire input into memory:
For large files, streams, or untrusted input, prefer streaming with io.Copy or process the data incrementally.
2. Reading untrusted input without a size limit
io.ReadAll has no built-in maximum size.
For input whose size is controlled by a remote client or external server, consider limiting it:
The limit should match the actual maximum size your application expects.
3. Converting to string unnecessarily
If the next API accepts []byte, keep the returned byte slice:
Only convert to string when a string is actually required.
4. Assuming io.ReadAll returns io.EOF on success
When the reader reaches EOF normally, io.ReadAll returns err == nil.
io.EOF is consumed as the normal end of input.
Rule of Thumb
Use
io.ReadAllwhen you intentionally want the entire input in memory; otherwise, keep the data streaming.