HowtoGo
Home / Standard Library / The io/ioutil Package
Standard Library

The io/ioutil Package

io/ioutil was where file helpers lived before Go 1.16 moved them into os and io. It is kept for compatibility and every function in it is deprecated. This page exists so you know what to reach for instead.

Every function in io/ioutil has been deprecated since Go 1.16. Nothing here is broken and old code still compiles, but new code should use the replacement named in each row.

Examples

Eight functions moved to two packages. The signatures are identical apart from ReadDir, so the change is a find and replace.

// Before                    After
ioutil.ReadFile(name)   //  os.ReadFile(name)
ioutil.WriteFile(...)   //  os.WriteFile(...)
ioutil.ReadAll(r)       //  io.ReadAll(r)
ioutil.ReadDir(dir)     //  os.ReadDir(dir)   *different return
ioutil.TempFile(...)    //  os.CreateTemp(...)
ioutil.TempDir(...)     //  os.MkdirTemp(...)
ioutil.NopCloser(r)     //  io.NopCloser(r)
ioutil.Discard          //  io.Discard
Output
$ grep -rl ioutil . | xargs sed -i 's/ioutil\.ReadFile/os.ReadFile/g'
$ goimports -w .
FunctionDescription
ReadFile(filename string) ([]byte, error)
os.ReadFile("config.yaml")
Deprecated since Go 1.16. Use os.ReadFile, which is the same function.
WriteFile(filename string, data []byte, perm fs.FileMode) error
os.WriteFile("out.txt", data, 0644)
Deprecated. Use os.WriteFile.
ReadAll(r io.Reader) ([]byte, error)
io.ReadAll(resp.Body)
Deprecated. Use io.ReadAll.
ReadDir(dirname string) ([]fs.FileInfo, error)
Deprecated. Use os.ReadDir, which returns []fs.DirEntry instead and is cheaper.
TempFile(dir, pattern string) (*os.File, error)
os.CreateTemp("", "upload-*.json")
Deprecated. Use os.CreateTemp.
TempDir(dir, pattern string) (string, error)
Deprecated. Use os.MkdirTemp. In a test, prefer t.TempDir, which cleans up for you.
NopCloser(r io.Reader) io.ReadCloser
Deprecated. Use io.NopCloser.
Discard
Deprecated. Use io.Discard.
Use instead: os io io/fs