os.ReadFile
The Go function that reads a whole file into memory in one call and returns its bytes and an error.
In more detail
It is ideal for small files such as configuration. For large or unbounded input, open the file and stream it instead, so memory use stays flat. Convert the bytes with string(data) to print them.
Example
back, err := os.ReadFile("probe.conf")Related terms
- os.WriteFile — The Go function that writes a slice of bytes to a file in one call, creating it with the permission mode you give if it does not exist.
- os.Open — The Go function that opens a file for reading and returns an *os.File you can stream from and must close.
- byte slice — A Go slice of bytes, the raw form data takes when it is read from or written to files, networks and encoders.
Taught in
- Files and the os Package — Concurrent Systems Programming with Go