Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Errors, I/O and Files

Go os.ReadFile() and os.WriteFile() functions

Reference for Go os.ReadFile and os.WriteFile: read or write an entire file in one call, file permissions, atomic-write pattern, errors and size caveats.

os.ReadFile reads an entire file into a []byte in one call. os.WriteFile does the reverse: it creates or truncates a file and writes a byte slice to it. Together they cover most small-file jobs (configuration, templates, JSON documents, test fixtures) without any explicit open, loop or close.

Both functions came from io/ioutil and moved into os in Go 1.16; the old ioutil.ReadFile and ioutil.WriteFile are just wrappers now and should not be used in new code.

Go
data, err := os.ReadFile("settings.json")
err = os.WriteFile("out.txt", []byte("hello\n"), 0o644)

Try it

Go Write, read back and append

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Handling errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func ReadFile(name string) ([]byte, error)
func WriteFile(name string, data []byte, perm FileMode) error
Name Description
name Path of the file, relative to the working directory or absolute.
data The bytes to write. A nil or empty slice creates or empties the file.
perm Permission bits used only if the file is created, such as 0o644 (owner read/write, others read).

Returns: ReadFile returns the full contents; on success the error is nil (reaching end of file is not reported as io.EOF). WriteFile returns nil or the first error.

Errors: a *fs.PathError wrapping fs.ErrNotExist, fs.ErrPermission, or the system error (for example "is a directory"). A failed WriteFile may leave a partially written file.

Usage notes

  • The whole file lives in memory. That is perfect for kilobytes and risky for gigabytes. Stream large files with os.Open plus bufio or io.Copy.
  • WriteFile truncates an existing file, and does not change its permissions. perm only matters when the file is created, and the process umask can clear bits.
  • Not atomic. A crash or a concurrent reader can see half a file. For important files write to a temp file in the same directory (os.CreateTemp), Sync, Close, then os.Rename over the target.
  • Appending, locking and random access are not supported; use os.OpenFile for those.
  • Text vs bytes: convert with string(data); no newline normalisation is done on any platform.
  • Secrets: use 0o600 for files holding credentials.
  • For files shipped inside the binary use embed, and for any fs.FS use fs.ReadFile(fsys, name).
  • os.ReadFile on special files (such as /proc entries) works because it grows its buffer as it reads rather than trusting the reported size.