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. 4 5 // Package ioutil implements some I/O utility functions. 6 // 7 // Deprecated: 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. 11 package ioutil 12 13 import ( 14 "io" 15 "io/fs" 16 "os" 17 "sort" 18 ) 19 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. 24 // 25 // Deprecated: As of Go 1.16, this function simply calls [io.ReadAll]. 26 func ReadAll(r io.Reader) ([]byte, error) { 27 return io.ReadAll(r) 28 } 29 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 33 // to be reported. 34 // 35 // Deprecated: As of Go 1.16, this function simply calls [os.ReadFile]. 36 func ReadFile(filename string) ([]byte, error) { 37 return os.ReadFile(filename) 38 } 39 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. 43 // 44 // Deprecated: 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) 47 } 48 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. 53 // 54 // Deprecated: 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 // 59 // If you must continue obtaining a list of [fs.FileInfo], you still can: 60 // 61 // entries, err := os.ReadDir(dirname) 62 // if err != nil { ... } 63 // infos := make([]fs.FileInfo, 0, len(entries)) 64 // for _, entry := range entries { 65 // info, err := entry.Info() 66 // if err != nil { ... } 67 // infos = append(infos, info) 68 // } 69 func ReadDir(dirname string) ([]fs.FileInfo, error) { 70 f, err := os.Open(dirname) 71 if err != nil { 72 return nil, err 73 } 74 list, err := f.Readdir(-1) 75 f.Close() 76 if err != nil { 77 return nil, err 78 } 79 sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() }) 80 return list, nil 81 } 82 83 // NopCloser returns a ReadCloser with a no-op Close method wrapping 84 // the provided Reader r. 85 // 86 // Deprecated: As of Go 1.16, this function simply calls [io.NopCloser]. 87 func NopCloser(r io.Reader) io.ReadCloser { 88 return io.NopCloser(r) 89 } 90 91 // Discard is an io.Writer on which all Write calls succeed 92 // without doing anything. 93 // 94 // Deprecated: As of Go 1.16, this value is simply [io.Discard]. 95 var Discard io.Writer = io.Discard 96