github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/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 f.Close() // ignore error; Write error takes precedence 32 log.Fatal(err) 33 } 34 if err := f.Close(); err != nil { 35 log.Fatal(err) 36 } 37 } 38 39 func ExampleChmod() { 40 if err := os.Chmod("some-filename", 0644); err != nil { 41 log.Fatal(err) 42 } 43 } 44 45 func ExampleChtimes() { 46 mtime := time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC) 47 atime := time.Date(2007, time.March, 2, 4, 5, 6, 0, time.UTC) 48 if err := os.Chtimes("some-filename", atime, mtime); err != nil { 49 log.Fatal(err) 50 } 51 } 52 53 func ExampleFileMode() { 54 fi, err := os.Lstat("some-filename") 55 if err != nil { 56 log.Fatal(err) 57 } 58 59 fmt.Printf("permissions: %#o\n", fi.Mode().Perm()) // 0400, 0777, etc. 60 switch mode := fi.Mode(); { 61 case mode.IsRegular(): 62 fmt.Println("regular file") 63 case mode.IsDir(): 64 fmt.Println("directory") 65 case mode&os.ModeSymlink != 0: 66 fmt.Println("symbolic link") 67 case mode&os.ModeNamedPipe != 0: 68 fmt.Println("named pipe") 69 } 70 } 71 72 func ExampleIsNotExist() { 73 filename := "a-nonexistent-file" 74 if _, err := os.Stat(filename); os.IsNotExist(err) { 75 fmt.Println("file does not exist") 76 } 77 // Output: 78 // file does not exist 79 } 80 81 func ExampleExpand() { 82 mapper := func(placeholderName string) string { 83 switch placeholderName { 84 case "DAY_PART": 85 return "morning" 86 case "NAME": 87 return "Gopher" 88 } 89 90 return "" 91 } 92 93 fmt.Println(os.Expand("Good ${DAY_PART}, $NAME!", mapper)) 94 95 // Output: 96 // Good morning, Gopher! 97 } 98 99 func ExampleExpandEnv() { 100 os.Setenv("NAME", "gopher") 101 os.Setenv("BURROW", "/usr/gopher") 102 103 fmt.Println(os.ExpandEnv("$NAME lives in ${BURROW}.")) 104 105 // Output: 106 // gopher lives in /usr/gopher. 107 } 108 109 func ExampleLookupEnv() { 110 show := func(key string) { 111 val, ok := os.LookupEnv(key) 112 if !ok { 113 fmt.Printf("%s not set\n", key) 114 } else { 115 fmt.Printf("%s=%s\n", key, val) 116 } 117 } 118 119 os.Setenv("SOME_KEY", "value") 120 os.Setenv("EMPTY_KEY", "") 121 122 show("SOME_KEY") 123 show("EMPTY_KEY") 124 show("MISSING_KEY") 125 126 // Output: 127 // SOME_KEY=value 128 // EMPTY_KEY= 129 // MISSING_KEY not set 130 } 131 132 func ExampleGetenv() { 133 os.Setenv("NAME", "gopher") 134 os.Setenv("BURROW", "/usr/gopher") 135 136 fmt.Printf("%s lives in %s.\n", os.Getenv("NAME"), os.Getenv("BURROW")) 137 138 // Output: 139 // gopher lives in /usr/gopher. 140 } 141 142 func ExampleUnsetenv() { 143 os.Setenv("TMPDIR", "/my/tmp") 144 defer os.Unsetenv("TMPDIR") 145 }