Source file
src/os/example_test.go
Documentation: os
1
2
3
4
5 package os_test
6
7 import (
8 "errors"
9 "fmt"
10 "io/fs"
11 "log"
12 "os"
13 "path/filepath"
14 "time"
15 )
16
17 func ExampleOpenFile() {
18 f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)
19 if err != nil {
20 log.Fatal(err)
21 }
22 if err := f.Close(); err != nil {
23 log.Fatal(err)
24 }
25 }
26
27 func ExampleOpenFile_append() {
28
29 f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
30 if err != nil {
31 log.Fatal(err)
32 }
33 if _, err := f.Write([]byte("appended some data\n")); err != nil {
34 f.Close()
35 log.Fatal(err)
36 }
37 if err := f.Close(); err != nil {
38 log.Fatal(err)
39 }
40 }
41
42 func ExampleChmod() {
43 if err := os.Chmod("some-filename", 0644); err != nil {
44 log.Fatal(err)
45 }
46 }
47
48 func ExampleChtimes() {
49 mtime := time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC)
50 atime := time.Date(2007, time.March, 2, 4, 5, 6, 0, time.UTC)
51 if err := os.Chtimes("some-filename", atime, mtime); err != nil {
52 log.Fatal(err)
53 }
54 }
55
56 func ExampleFileMode() {
57 fi, err := os.Lstat("some-filename")
58 if err != nil {
59 log.Fatal(err)
60 }
61
62 fmt.Printf("permissions: %#o\n", fi.Mode().Perm())
63 switch mode := fi.Mode(); {
64 case mode.IsRegular():
65 fmt.Println("regular file")
66 case mode.IsDir():
67 fmt.Println("directory")
68 case mode&fs.ModeSymlink != 0:
69 fmt.Println("symbolic link")
70 case mode&fs.ModeNamedPipe != 0:
71 fmt.Println("named pipe")
72 }
73 }
74
75 func ExampleErrNotExist() {
76 filename := "a-nonexistent-file"
77 if _, err := os.Stat(filename); errors.Is(err, fs.ErrNotExist) {
78 fmt.Println("file does not exist")
79 }
80
81
82 }
83
84 func ExampleExpand() {
85 mapper := func(placeholderName string) string {
86 switch placeholderName {
87 case "DAY_PART":
88 return "morning"
89 case "NAME":
90 return "Gopher"
91 }
92
93 return ""
94 }
95
96 fmt.Println(os.Expand("Good ${DAY_PART}, $NAME!", mapper))
97
98
99
100 }
101
102 func ExampleExpandEnv() {
103 os.Setenv("NAME", "gopher")
104 os.Setenv("BURROW", "/usr/gopher")
105
106 fmt.Println(os.ExpandEnv("$NAME lives in ${BURROW}."))
107
108
109
110 }
111
112 func ExampleLookupEnv() {
113 show := func(key string) {
114 val, ok := os.LookupEnv(key)
115 if !ok {
116 fmt.Printf("%s not set\n", key)
117 } else {
118 fmt.Printf("%s=%s\n", key, val)
119 }
120 }
121
122 os.Setenv("SOME_KEY", "value")
123 os.Setenv("EMPTY_KEY", "")
124
125 show("SOME_KEY")
126 show("EMPTY_KEY")
127 show("MISSING_KEY")
128
129
130
131
132
133 }
134
135 func ExampleGetenv() {
136 os.Setenv("NAME", "gopher")
137 os.Setenv("BURROW", "/usr/gopher")
138
139 fmt.Printf("%s lives in %s.\n", os.Getenv("NAME"), os.Getenv("BURROW"))
140
141
142
143 }
144
145 func ExampleUnsetenv() {
146 os.Setenv("TMPDIR", "/my/tmp")
147 defer os.Unsetenv("TMPDIR")
148 }
149
150 func ExampleReadDir() {
151 files, err := os.ReadDir(".")
152 if err != nil {
153 log.Fatal(err)
154 }
155
156 for _, file := range files {
157 fmt.Println(file.Name())
158 }
159 }
160
161 func ExampleMkdirTemp() {
162 dir, err := os.MkdirTemp("", "example")
163 if err != nil {
164 log.Fatal(err)
165 }
166 defer os.RemoveAll(dir)
167
168 file := filepath.Join(dir, "tmpfile")
169 if err := os.WriteFile(file, []byte("content"), 0666); err != nil {
170 log.Fatal(err)
171 }
172 }
173
174 func ExampleMkdirTemp_suffix() {
175 logsDir, err := os.MkdirTemp("", "*-logs")
176 if err != nil {
177 log.Fatal(err)
178 }
179 defer os.RemoveAll(logsDir)
180
181
182
183 globPattern := filepath.Join(os.TempDir(), "*-logs")
184 matches, err := filepath.Glob(globPattern)
185 if err != nil {
186 log.Fatalf("Failed to match %q: %v", globPattern, err)
187 }
188
189 for _, match := range matches {
190 if err := os.RemoveAll(match); err != nil {
191 log.Printf("Failed to remove %q: %v", match, err)
192 }
193 }
194 }
195
196 func ExampleCreateTemp() {
197 f, err := os.CreateTemp("", "example")
198 if err != nil {
199 log.Fatal(err)
200 }
201 defer os.Remove(f.Name())
202
203 if _, err := f.Write([]byte("content")); err != nil {
204 log.Fatal(err)
205 }
206 if err := f.Close(); err != nil {
207 log.Fatal(err)
208 }
209 }
210
211 func ExampleCreateTemp_suffix() {
212 f, err := os.CreateTemp("", "example.*.txt")
213 if err != nil {
214 log.Fatal(err)
215 }
216 defer os.Remove(f.Name())
217
218 if _, err := f.Write([]byte("content")); err != nil {
219 f.Close()
220 log.Fatal(err)
221 }
222 if err := f.Close(); err != nil {
223 log.Fatal(err)
224 }
225 }
226
227 func ExampleReadFile() {
228 data, err := os.ReadFile("testdata/hello")
229 if err != nil {
230 log.Fatal(err)
231 }
232 os.Stdout.Write(data)
233
234
235
236 }
237
238 func ExampleWriteFile() {
239 err := os.WriteFile("testdata/hello", []byte("Hello, Gophers!"), 0666)
240 if err != nil {
241 log.Fatal(err)
242 }
243 }
244
View as plain text