ioutil.mx raw

   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  	"slices"
  18  	"bytes"
  19  )
  20  
  21  // ReadAll reads from r until an error or EOF and returns the data it read.
  22  // A successful call returns err == nil, not err == EOF. Because ReadAll is
  23  // defined to read from src until EOF, it does not treat an EOF from Read
  24  // as an error to be reported.
  25  //
  26  // Deprecated: As of Go 1.16, this function simply calls [io.ReadAll].
  27  //
  28  //go:fix inline
  29  func ReadAll(r io.Reader) ([]byte, error) {
  30  	return io.ReadAll(r)
  31  }
  32  
  33  // ReadFile reads the file named by filename and returns the contents.
  34  // A successful call returns err == nil, not err == EOF. Because ReadFile
  35  // reads the whole file, it does not treat an EOF from Read as an error
  36  // to be reported.
  37  //
  38  // Deprecated: As of Go 1.16, this function simply calls [os.ReadFile].
  39  //
  40  //go:fix inline
  41  func ReadFile(filename []byte) ([]byte, error) {
  42  	return os.ReadFile(filename)
  43  }
  44  
  45  // WriteFile writes data to a file named by filename.
  46  // If the file does not exist, WriteFile creates it with permissions perm
  47  // (before umask); otherwise WriteFile truncates it before writing, without changing permissions.
  48  //
  49  // Deprecated: As of Go 1.16, this function simply calls [os.WriteFile].
  50  //
  51  //go:fix inline
  52  func WriteFile(filename []byte, data []byte, perm fs.FileMode) error {
  53  	return os.WriteFile(filename, data, perm)
  54  }
  55  
  56  // ReadDir reads the directory named by dirname and returns
  57  // a list of fs.FileInfo for the directory's contents,
  58  // sorted by filename. If an error occurs reading the directory,
  59  // ReadDir returns no directory entries along with the error.
  60  //
  61  // Deprecated: As of Go 1.16, [os.ReadDir] is a more efficient and correct choice:
  62  // it returns a list of [fs.DirEntry] instead of [fs.FileInfo],
  63  // and it returns partial results in the case of an error
  64  // midway through reading a directory.
  65  //
  66  // If you must continue obtaining a list of [fs.FileInfo], you still can:
  67  //
  68  //	entries, err := os.ReadDir(dirname)
  69  //	if err != nil { ... }
  70  //	infos := make([]fs.FileInfo, 0, len(entries))
  71  //	for _, entry := range entries {
  72  //		info, err := entry.Info()
  73  //		if err != nil { ... }
  74  //		infos = append(infos, info)
  75  //	}
  76  func ReadDir(dirname []byte) ([]fs.FileInfo, error) {
  77  	f, err := os.Open(dirname)
  78  	if err != nil {
  79  		return nil, err
  80  	}
  81  	list, err := f.Readdir(-1)
  82  	f.Close()
  83  	if err != nil {
  84  		return nil, err
  85  	}
  86  	slices.SortFunc(list, func(a, b os.FileInfo) int {
  87  		return bytes.Compare(a.Name(), b.Name())
  88  	})
  89  	return list, nil
  90  }
  91  
  92  // NopCloser returns a ReadCloser with a no-op Close method wrapping
  93  // the provided Reader r.
  94  //
  95  // Deprecated: As of Go 1.16, this function simply calls [io.NopCloser].
  96  //
  97  //go:fix inline
  98  func NopCloser(r io.Reader) io.ReadCloser {
  99  	return io.NopCloser(r)
 100  }
 101  
 102  // Discard is an io.Writer on which all Write calls succeed
 103  // without doing anything.
 104  //
 105  // Deprecated: As of Go 1.16, this value is simply [io.Discard].
 106  var Discard io.Writer = io.Discard
 107