Source file src/io/ioutil/example_test.go

     1  // Copyright 2015 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_test
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"log"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  )
    15  
    16  func ExampleReadAll() {
    17  	r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.")
    18  
    19  	b, err := ioutil.ReadAll(r)
    20  	if err != nil {
    21  		log.Fatal(err)
    22  	}
    23  
    24  	fmt.Printf("%s", b)
    25  
    26  	// Output:
    27  	// Go is a general-purpose language designed with systems programming in mind.
    28  }
    29  
    30  func ExampleReadDir() {
    31  	files, err := ioutil.ReadDir(".")
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  
    36  	for _, file := range files {
    37  		fmt.Println(file.Name())
    38  	}
    39  }
    40  
    41  func ExampleTempDir() {
    42  	content := []byte("temporary file's content")
    43  	dir, err := ioutil.TempDir("", "example")
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  
    48  	defer os.RemoveAll(dir) // clean up
    49  
    50  	tmpfn := filepath.Join(dir, "tmpfile")
    51  	if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {
    52  		log.Fatal(err)
    53  	}
    54  }
    55  
    56  func ExampleTempDir_suffix() {
    57  	parentDir := os.TempDir()
    58  	logsDir, err := ioutil.TempDir(parentDir, "*-logs")
    59  	if err != nil {
    60  		log.Fatal(err)
    61  	}
    62  	defer os.RemoveAll(logsDir) // clean up
    63  
    64  	// Logs can be cleaned out earlier if needed by searching
    65  	// for all directories whose suffix ends in *-logs.
    66  	globPattern := filepath.Join(parentDir, "*-logs")
    67  	matches, err := filepath.Glob(globPattern)
    68  	if err != nil {
    69  		log.Fatalf("Failed to match %q: %v", globPattern, err)
    70  	}
    71  
    72  	for _, match := range matches {
    73  		if err := os.RemoveAll(match); err != nil {
    74  			log.Printf("Failed to remove %q: %v", match, err)
    75  		}
    76  	}
    77  }
    78  
    79  func ExampleTempFile() {
    80  	content := []byte("temporary file's content")
    81  	tmpfile, err := ioutil.TempFile("", "example")
    82  	if err != nil {
    83  		log.Fatal(err)
    84  	}
    85  
    86  	defer os.Remove(tmpfile.Name()) // clean up
    87  
    88  	if _, err := tmpfile.Write(content); err != nil {
    89  		log.Fatal(err)
    90  	}
    91  	if err := tmpfile.Close(); err != nil {
    92  		log.Fatal(err)
    93  	}
    94  }
    95  
    96  func ExampleTempFile_suffix() {
    97  	content := []byte("temporary file's content")
    98  	tmpfile, err := ioutil.TempFile("", "example.*.txt")
    99  	if err != nil {
   100  		log.Fatal(err)
   101  	}
   102  
   103  	defer os.Remove(tmpfile.Name()) // clean up
   104  
   105  	if _, err := tmpfile.Write(content); err != nil {
   106  		tmpfile.Close()
   107  		log.Fatal(err)
   108  	}
   109  	if err := tmpfile.Close(); err != nil {
   110  		log.Fatal(err)
   111  	}
   112  }
   113  
   114  func ExampleReadFile() {
   115  	content, err := ioutil.ReadFile("testdata/hello")
   116  	if err != nil {
   117  		log.Fatal(err)
   118  	}
   119  
   120  	fmt.Printf("File contents: %s", content)
   121  
   122  	// Output:
   123  	// File contents: Hello, Gophers!
   124  }
   125  
   126  func ExampleWriteFile() {
   127  	message := []byte("Hello, Gophers!")
   128  	err := ioutil.WriteFile("hello", message, 0644)
   129  	if err != nil {
   130  		log.Fatal(err)
   131  	}
   132  }
   133  

View as plain text