summaryrefslogtreecommitdiff
path: root/devdocs/go/io%2Fioutil%2Findex.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/go/io%2Fioutil%2Findex.html')
-rw-r--r--devdocs/go/io%2Fioutil%2Findex.html147
1 files changed, 147 insertions, 0 deletions
diff --git a/devdocs/go/io%2Fioutil%2Findex.html b/devdocs/go/io%2Fioutil%2Findex.html
new file mode 100644
index 00000000..072cc201
--- /dev/null
+++ b/devdocs/go/io%2Fioutil%2Findex.html
@@ -0,0 +1,147 @@
+<h1> Package ioutil </h1> <ul id="short-nav">
+<li><code>import "io/ioutil"</code></li>
+<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
+<li><a href="#pkg-index" class="indexLink">Index</a></li>
+<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
+</ul> <h2 id="pkg-overview">Overview </h2> <p>Package ioutil implements some I/O utility functions. </p>
+<p>Deprecated: As of Go 1.16, the same functionality is now provided by package <span>io</span> or package <span>os</span>, and those implementations should be preferred in new code. See the specific function documentation for details. </p> <h2 id="pkg-index">Index </h2> <ul id="manual-nav">
+<li><a href="#pkg-variables">Variables</a></li>
+<li><a href="#NopCloser">func NopCloser(r io.Reader) io.ReadCloser</a></li>
+<li><a href="#ReadAll">func ReadAll(r io.Reader) ([]byte, error)</a></li>
+<li><a href="#ReadDir">func ReadDir(dirname string) ([]fs.FileInfo, error)</a></li>
+<li><a href="#ReadFile">func ReadFile(filename string) ([]byte, error)</a></li>
+<li><a href="#TempDir">func TempDir(dir, pattern string) (name string, err error)</a></li>
+<li><a href="#TempFile">func TempFile(dir, pattern string) (f *os.File, err error)</a></li>
+<li><a href="#WriteFile">func WriteFile(filename string, data []byte, perm fs.FileMode) error</a></li>
+</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_ReadAll">ReadAll</a></dd> <dd><a class="exampleLink" href="#example_ReadDir">ReadDir</a></dd> <dd><a class="exampleLink" href="#example_ReadFile">ReadFile</a></dd> <dd><a class="exampleLink" href="#example_TempDir">TempDir</a></dd> <dd><a class="exampleLink" href="#example_TempDir_suffix">TempDir (Suffix)</a></dd> <dd><a class="exampleLink" href="#example_TempFile">TempFile</a></dd> <dd><a class="exampleLink" href="#example_TempFile_suffix">TempFile (Suffix)</a></dd> <dd><a class="exampleLink" href="#example_WriteFile">WriteFile</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>ioutil.go</span> <span>tempfile.go</span> </p> <h2 id="pkg-variables">Variables</h2> <p>Discard is an io.Writer on which all Write calls succeed without doing anything. </p>
+<p>Deprecated: As of Go 1.16, this value is simply <span>io.Discard</span>. </p>
+<pre data-language="go">var Discard io.Writer = io.Discard</pre> <h2 id="NopCloser">func <span>NopCloser</span> </h2> <pre data-language="go">func NopCloser(r io.Reader) io.ReadCloser</pre> <p>NopCloser returns a ReadCloser with a no-op Close method wrapping the provided Reader r. </p>
+<p>Deprecated: As of Go 1.16, this function simply calls <span>io.NopCloser</span>. </p>
+<h2 id="ReadAll">func <span>ReadAll</span> </h2> <pre data-language="go">func ReadAll(r io.Reader) ([]byte, error)</pre> <p>ReadAll reads from r until an error or EOF and returns the data it read. A successful call returns err == nil, not err == EOF. Because ReadAll is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported. </p>
+<p>Deprecated: As of Go 1.16, this function simply calls <span>io.ReadAll</span>. </p> <h4 id="example_ReadAll"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.")
+
+b, err := ioutil.ReadAll(r)
+if err != nil {
+ log.Fatal(err)
+}
+
+fmt.Printf("%s", b)
+
+</pre> <p>Output:</p> <pre class="output" data-language="go">Go is a general-purpose language designed with systems programming in mind.
+</pre> <h2 id="ReadDir">func <span>ReadDir</span> </h2> <pre data-language="go">func ReadDir(dirname string) ([]fs.FileInfo, error)</pre> <p>ReadDir reads the directory named by dirname and returns a list of fs.FileInfo for the directory's contents, sorted by filename. If an error occurs reading the directory, ReadDir returns no directory entries along with the error. </p>
+<p>Deprecated: As of Go 1.16, <span>os.ReadDir</span> is a more efficient and correct choice: it returns a list of <span>fs.DirEntry</span> instead of <span>fs.FileInfo</span>, and it returns partial results in the case of an error midway through reading a directory. </p>
+<p>If you must continue obtaining a list of <span>fs.FileInfo</span>, you still can: </p>
+<pre data-language="go">entries, err := os.ReadDir(dirname)
+if err != nil { ... }
+infos := make([]fs.FileInfo, 0, len(entries))
+for _, entry := range entries {
+ info, err := entry.Info()
+ if err != nil { ... }
+ infos = append(infos, info)
+}
+</pre> <h4 id="example_ReadDir"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">
+files, err := ioutil.ReadDir(".")
+if err != nil {
+ log.Fatal(err)
+}
+
+for _, file := range files {
+ fmt.Println(file.Name())
+}
+</pre> <h2 id="ReadFile">func <span>ReadFile</span> </h2> <pre data-language="go">func ReadFile(filename string) ([]byte, error)</pre> <p>ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported. </p>
+<p>Deprecated: As of Go 1.16, this function simply calls <span>os.ReadFile</span>. </p> <h4 id="example_ReadFile"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">content, err := ioutil.ReadFile("testdata/hello")
+if err != nil {
+ log.Fatal(err)
+}
+
+fmt.Printf("File contents: %s", content)
+
+</pre> <p>Output:</p> <pre class="output" data-language="go">File contents: Hello, Gophers!
+</pre> <h2 id="TempDir">func <span>TempDir</span> </h2> <pre data-language="go">func TempDir(dir, pattern string) (name string, err error)</pre> <p>TempDir creates a new temporary directory in the directory dir. The directory name is generated by taking pattern and applying a random string to the end. If pattern includes a "*", the random string replaces the last "*". TempDir returns the name of the new directory. If dir is the empty string, TempDir uses the default directory for temporary files (see <span>os.TempDir</span>). Multiple programs calling TempDir simultaneously will not choose the same directory. It is the caller's responsibility to remove the directory when no longer needed. </p>
+<p>Deprecated: As of Go 1.17, this function simply calls <span>os.MkdirTemp</span>. </p> <h4 id="example_TempDir"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">
+content := []byte("temporary file's content")
+dir, err := ioutil.TempDir("", "example")
+if err != nil {
+ log.Fatal(err)
+}
+
+defer os.RemoveAll(dir) // clean up
+
+tmpfn := filepath.Join(dir, "tmpfile")
+if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {
+ log.Fatal(err)
+}
+</pre> <h4 id="example_TempDir_suffix"> <span class="text">Example (Suffix)</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">
+parentDir := os.TempDir()
+logsDir, err := ioutil.TempDir(parentDir, "*-logs")
+if err != nil {
+ log.Fatal(err)
+}
+defer os.RemoveAll(logsDir) // clean up
+
+// Logs can be cleaned out earlier if needed by searching
+// for all directories whose suffix ends in *-logs.
+globPattern := filepath.Join(parentDir, "*-logs")
+matches, err := filepath.Glob(globPattern)
+if err != nil {
+ log.Fatalf("Failed to match %q: %v", globPattern, err)
+}
+
+for _, match := range matches {
+ if err := os.RemoveAll(match); err != nil {
+ log.Printf("Failed to remove %q: %v", match, err)
+ }
+}
+</pre> <h2 id="TempFile">func <span>TempFile</span> </h2> <pre data-language="go">func TempFile(dir, pattern string) (f *os.File, err error)</pre> <p>TempFile creates a new temporary file in the directory dir, opens the file for reading and writing, and returns the resulting *<span>os.File</span>. The filename is generated by taking pattern and adding a random string to the end. If pattern includes a "*", the random string replaces the last "*". If dir is the empty string, TempFile uses the default directory for temporary files (see <span>os.TempDir</span>). Multiple programs calling TempFile simultaneously will not choose the same file. The caller can use f.Name() to find the pathname of the file. It is the caller's responsibility to remove the file when no longer needed. </p>
+<p>Deprecated: As of Go 1.17, this function simply calls <span>os.CreateTemp</span>. </p> <h4 id="example_TempFile"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">
+content := []byte("temporary file's content")
+tmpfile, err := ioutil.TempFile("", "example")
+if err != nil {
+ log.Fatal(err)
+}
+
+defer os.Remove(tmpfile.Name()) // clean up
+
+if _, err := tmpfile.Write(content); err != nil {
+ log.Fatal(err)
+}
+if err := tmpfile.Close(); err != nil {
+ log.Fatal(err)
+}
+</pre> <h4 id="example_TempFile_suffix"> <span class="text">Example (Suffix)</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">
+content := []byte("temporary file's content")
+tmpfile, err := ioutil.TempFile("", "example.*.txt")
+if err != nil {
+ log.Fatal(err)
+}
+
+defer os.Remove(tmpfile.Name()) // clean up
+
+if _, err := tmpfile.Write(content); err != nil {
+ tmpfile.Close()
+ log.Fatal(err)
+}
+if err := tmpfile.Close(); err != nil {
+ log.Fatal(err)
+}
+</pre> <h2 id="WriteFile">func <span>WriteFile</span> </h2> <pre data-language="go">func WriteFile(filename string, data []byte, perm fs.FileMode) error</pre> <p>WriteFile writes data to a file named by filename. If the file does not exist, WriteFile creates it with permissions perm (before umask); otherwise WriteFile truncates it before writing, without changing permissions. </p>
+<p>Deprecated: As of Go 1.16, this function simply calls <span>os.WriteFile</span>. </p> <h4 id="example_WriteFile"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">
+message := []byte("Hello, Gophers!")
+err := ioutil.WriteFile("hello", message, 0644)
+if err != nil {
+ log.Fatal(err)
+}
+</pre><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
+ <a href="http://golang.org/pkg/io/ioutil/" class="_attribution-link">http://golang.org/pkg/io/ioutil/</a>
+ </p>
+</div>