1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package ioutil implements some I/O utility functions.
7 // As of Go 1.16, the same functionality is now provided
8 // by package io or package os, and those implementations
9 // should be preferred in new code.
10 // See the specific function documentation for details.
20 // ReadAll reads from r until an error or EOF and returns the data it read.
21 // A successful call returns err == nil, not err == EOF. Because ReadAll is
22 // defined to read from src until EOF, it does not treat an EOF from Read
23 // as an error to be reported.
25 // As of Go 1.16, this function simply calls io.ReadAll.
26 func ReadAll(r io
.Reader
) ([]byte, error
) {
30 // ReadFile reads the file named by filename and returns the contents.
31 // A successful call returns err == nil, not err == EOF. Because ReadFile
32 // reads the whole file, it does not treat an EOF from Read as an error
35 // As of Go 1.16, this function simply calls os.ReadFile.
36 func ReadFile(filename
string) ([]byte, error
) {
37 return os
.ReadFile(filename
)
40 // WriteFile writes data to a file named by filename.
41 // If the file does not exist, WriteFile creates it with permissions perm
42 // (before umask); otherwise WriteFile truncates it before writing, without changing permissions.
44 // As of Go 1.16, this function simply calls os.WriteFile.
45 func WriteFile(filename
string, data
[]byte, perm fs
.FileMode
) error
{
46 return os
.WriteFile(filename
, data
, perm
)
49 // ReadDir reads the directory named by dirname and returns
50 // a list of fs.FileInfo for the directory's contents,
51 // sorted by filename. If an error occurs reading the directory,
52 // ReadDir returns no directory entries along with the error.
54 // As of Go 1.16, os.ReadDir is a more efficient and correct choice:
55 // it returns a list of fs.DirEntry instead of fs.FileInfo,
56 // and it returns partial results in the case of an error
57 // midway through reading a directory.
58 func ReadDir(dirname
string) ([]fs
.FileInfo
, error
) {
59 f
, err
:= os
.Open(dirname
)
63 list
, err
:= f
.Readdir(-1)
68 sort
.Slice(list
, func(i
, j
int) bool { return list
[i
].Name() < list
[j
].Name() })
72 // NopCloser returns a ReadCloser with a no-op Close method wrapping
73 // the provided Reader r.
75 // As of Go 1.16, this function simply calls io.NopCloser.
76 func NopCloser(r io
.Reader
) io
.ReadCloser
{
77 return io
.NopCloser(r
)
80 // Discard is an io.Writer on which all Write calls succeed
81 // without doing anything.
83 // As of Go 1.16, this value is simply io.Discard.
84 var Discard io
.Writer
= io
.Discard