Go's standard library handles the file system through os for files and directories, io and bufio for reading and writing, path/filepath for portable paths, and io/fs for an abstract file system interface. Most tasks need only os.ReadFile, os.WriteFile, os.MkdirAll, os.ReadDir and filepath.WalkDir.
This guide shows these operations with current idioms, then covers streaming large files, safe writes, temporary files and embedding files in the binary.
io/fs interface, embed and os.RootFor small files, read or write everything in one call. These functions replaced the deprecated ioutil helpers in Go 1.16.
os.WriteFile creates the file if needed and truncates it otherwise. The 0o644 permission is subject to the process umask.
For large files, open a handle and process it in chunks so memory stays flat.
Use os.Open for reading, os.Create for writing a new or truncated file, and os.OpenFile with flags such as os.O_APPEND|os.O_CREATE|os.O_WRONLY for appending. To copy a file, use io.Copy(dst, src). When you write, check the error from Close, because buffered data may only fail at that point.
Use path/filepath for OS paths, never string concatenation, so separators are correct on Windows and Unix.
To walk a tree, use filepath.WalkDir, which is faster than the older filepath.Walk because it does not call Stat on every entry.
os.Stat returns size, mode and modification time. Test for errors with errors.Is rather than comparing strings.
| Operation | Function |
|---|---|
| Rename or move | os.Rename |
| Delete a file or empty directory | os.Remove |
| Delete a tree | os.RemoveAll |
| Change permissions | os.Chmod |
| Check existence | os.Stat with fs.ErrNotExist |
Avoid check-then-act code such as "if not exists then create", which has a race. Use os.OpenFile with os.O_EXCL to create a file atomically.
Create temporary files with os.CreateTemp and directories with os.MkdirTemp. To avoid leaving a half-written file after a crash, write to a temporary file in the same directory and rename it.
The rename is atomic on the same file system on Unix. In tests, use t.TempDir(), which cleans up automatically.
io/fs interface, embed and os.Rootio/fs.FS is a read-only file system interface. os.DirFS("dir") exposes a directory as an fs.FS, and testing/fstest.MapFS provides an in-memory one for tests. Code that accepts fs.FS works on disk, in memory or embedded.
The embed package bundles files into the binary at build time:
When you open paths that come from users, guard against .. traversal. Go 1.24 added os.OpenRoot and os.Root, which restrict all operations to a directory and reject paths that escape it, including through symbolic links.
ioutil still usable?It still compiles but is deprecated since Go 1.16. Use os.ReadFile, os.WriteFile, os.ReadDir and os.CreateTemp instead.
Call os.Stat and test the error with errors.Is(err, fs.ErrNotExist). Treat other errors, such as permission problems, separately.
filepath.Join or path.Join?Use filepath.Join for OS paths. Use path.Join for slash-separated paths such as URLs and io/fs names.
Stream it with bufio.Scanner, bufio.Reader or io.Copy instead of os.ReadFile.
Use os.ReadFile and os.WriteFile for small files, buffered streams for large ones, filepath for portable paths and filepath.WalkDir for traversal. Write atomically through a temporary file and rename, accept fs.FS in your APIs, and use os.Root when handling untrusted paths.