...

Source file src/io/ioutil/ioutil.go

Documentation: io/ioutil

		 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  // 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  // 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  // 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  // 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  // 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)
		60  	if err != nil {
		61  		return nil, err
		62  	}
		63  	list, err := f.Readdir(-1)
		64  	f.Close()
		65  	if err != nil {
		66  		return nil, err
		67  	}
		68  	sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
		69  	return list, nil
		70  }
		71  
		72  // NopCloser returns a ReadCloser with a no-op Close method wrapping
		73  // the provided Reader r.
		74  //
		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)
		78  }
		79  
		80  // Discard is an io.Writer on which all Write calls succeed
		81  // without doing anything.
		82  //
		83  // As of Go 1.16, this value is simply io.Discard.
		84  var Discard io.Writer = io.Discard
		85  

View as plain text