github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/os/example_test.go (about)

     1  // Copyright 2016 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 os_test
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"os"
    11  	"time"
    12  )
    13  
    14  func ExampleOpenFile() {
    15  	f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)
    16  	if err != nil {
    17  		log.Fatal(err)
    18  	}
    19  	if err := f.Close(); err != nil {
    20  		log.Fatal(err)
    21  	}
    22  }
    23  
    24  func ExampleOpenFile_append() {
    25  	// If the file doesn't exist, create it, or append to the file
    26  	f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  	if _, err := f.Write([]byte("appended some data\n")); err != nil {
    31  		log.Fatal(err)
    32  	}
    33  	if err := f.Close(); err != nil {
    34  		log.Fatal(err)
    35  	}
    36  }
    37  
    38  func ExampleChmod() {
    39  	if err := os.Chmod("some-filename", 0644); err != nil {
    40  		log.Fatal(err)
    41  	}
    42  }
    43  
    44  func ExampleChtimes() {
    45  	mtime := time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC)
    46  	atime := time.Date(2007, time.March, 2, 4, 5, 6, 0, time.UTC)
    47  	if err := os.Chtimes("some-filename", atime, mtime); err != nil {
    48  		log.Fatal(err)
    49  	}
    50  }
    51  
    52  func ExampleFileMode() {
    53  	fi, err := os.Lstat("some-filename")
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  
    58  	fmt.Printf("permissions: %#o\n", fi.Mode().Perm()) // 0400, 0777, etc.
    59  	switch mode := fi.Mode(); {
    60  	case mode.IsRegular():
    61  		fmt.Println("regular file")
    62  	case mode.IsDir():
    63  		fmt.Println("directory")
    64  	case mode&os.ModeSymlink != 0:
    65  		fmt.Println("symbolic link")
    66  	case mode&os.ModeNamedPipe != 0:
    67  		fmt.Println("named pipe")
    68  	}
    69  }
    70  
    71  func ExampleIsNotExist() {
    72  	filename := "a-nonexistent-file"
    73  	if _, err := os.Stat(filename); os.IsNotExist(err) {
    74  		fmt.Println("file does not exist")
    75  	}
    76  	// Output:
    77  	// file does not exist
    78  }
    79  
    80  func ExampleExpand() {
    81  	mapper := func(placeholderName string) string {
    82  		switch placeholderName {
    83  		case "DAY_PART":
    84  			return "morning"
    85  		case "NAME":
    86  			return "Gopher"
    87  		}
    88  
    89  		return ""
    90  	}
    91  
    92  	fmt.Println(os.Expand("Good ${DAY_PART}, $NAME!", mapper))
    93  
    94  	// Output:
    95  	// Good morning, Gopher!
    96  }
    97  
    98  func ExampleExpandEnv() {
    99  	os.Setenv("NAME", "gopher")
   100  	os.Setenv("BURROW", "/usr/gopher")
   101  
   102  	fmt.Println(os.ExpandEnv("$NAME lives in ${BURROW}."))
   103  
   104  	// Output:
   105  	// gopher lives in /usr/gopher.
   106  }
   107  
   108  func ExampleLookupEnv() {
   109  	show := func(key string) {
   110  		val, ok := os.LookupEnv(key)
   111  		if !ok {
   112  			fmt.Printf("%s not set\n", key)
   113  		} else {
   114  			fmt.Printf("%s=%s\n", key, val)
   115  		}
   116  	}
   117  
   118  	os.Setenv("SOME_KEY", "value")
   119  	os.Setenv("EMPTY_KEY", "")
   120  
   121  	show("SOME_KEY")
   122  	show("EMPTY_KEY")
   123  	show("MISSING_KEY")
   124  
   125  	// Output:
   126  	// SOME_KEY=value
   127  	// EMPTY_KEY=
   128  	// MISSING_KEY not set
   129  }
   130  
   131  func ExampleGetenv() {
   132  	os.Setenv("NAME", "gopher")
   133  	os.Setenv("BURROW", "/usr/gopher")
   134  
   135  	fmt.Printf("%s lives in %s.\n", os.Getenv("NAME"), os.Getenv("BURROW"))
   136  
   137  	// Output:
   138  	// gopher lives in /usr/gopher.
   139  }
   140  
   141  func ExampleUnsetenv() {
   142  	os.Setenv("TMPDIR", "/my/tmp")
   143  	defer os.Unsetenv("TMPDIR")
   144  }